从输入文件java中分割二维数组

时间:2018-11-18 06:33:53

标签: java arrays custom-sections

有人可以帮我吗?共有3个部分,第1部分是第一个索引,子连续部分是其后每5个索引的总和。不知道为什么我总是收到异常错误。

public class Main {
public static void main(String[] args) throws IOException {
    double min, max, sum = 0, avg = 0;// DecimalFormat class is used to format the output
    DecimalFormat df = new DecimalFormat(".0");// DecimalFormat class is used to format the output
    DecimalFormat df1 = new DecimalFormat(".00");
    int rows = 0, cols = 0;
    String names[] = null;
    double scores[][] = null;
    double scoressec[][] = null;

    BufferedWriter writer = null;
    try { //Opening the input file
        Scanner sc = new Scanner(new File("Input.txt"));//Reading the no of rows and columns
        rows = sc.nextInt();//check
        cols = sc.nextInt();
        names = new String[rows];
        scores = new double[rows][cols - 1];
        scoressec = new double[rows][0];
        /* Getting the data from the input file
         * and populate those values into array
         */
        for (int i = 0; i < rows; i++) {
            names[i] = sc.next();
            for (int j = 0; j < cols - 1; j++) {
                scores[i][j] = sc.nextDouble();

                if (j == 0) {
                    scoressec[i][1] = scores[i][1];
                } else if (j == 5) {
                    scoressec[i][2] += scores[i][j] - scores[i][1];
                } else {
                    scoressec[i][3] += scores[i][j] - scoressec[i][2];
                }
            }


        } //closing the input file`

0 个答案:

没有答案