Java:在输出文件中获得零

时间:2016-10-23 11:41:04

标签: java multidimensional-array

编辑:我修复了原来的异常错误,感谢Tim Biegeleisen在我在下面添加注释的行中添加了if(scan.hasNextInt())。但是输出文件给了我零而不是预期的结果。

我知道我的代码是非常业余的,但是请耐心等待我,因为我还只是一个初学者。非常感谢你提前!

EDIT2: 它输入的文本文件的结构如下:

50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87
50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87
50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87
50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87
50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87
50 50 50 50 50 50
65 73 45 98 90 76
33 90 75 34 42 55
56 86 88 99 23 97
65 78 79 98 70 87

EDIT3:似乎问题在于,由于某种原因,它并没有超出if的范围。

2 个答案:

答案 0 :(得分:0)

当Scanner没有(在这种情况下)int以便读取时,抛出NoSuchElementException。

我不知道.txt文件的结构,所以如果你可以将它添加到你的问题中会很棒。

解决方案可能是通过执行以下操作来重置扫描仪的偏移:

scan.close();
scan = new Scanner(f);
for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 6; j++) {
        subjects[i][j] = scan.nextInt();
    }
}

编辑:

这段代码对我有用:

public static void main(String[] args) throws Exception {
    Scanner scan = new Scanner(new FileInputStream(new File("file.txt")));
    PrintWriter p = new PrintWriter("Class_report.txt");
    int x = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
    double avg1 = 0, avg2 = 0, avg3 = 0, avg4 = 0, avg5 = 0, avg6 = 0;
    int[] total = new int[30];
    int[] number = new int[6];
    int[][] subjects = new int[30][6];
    double[] average = new double[6];
    for (int i = 0; i < 30; i++) {
        for (int j = 0; j < 6; j++) {
            x = x + scan.nextInt();
        }
        total[i] = x;
        x = 0;
    }

    scan = new Scanner(new FileInputStream(new File("file.txt")));
    for (int i = 0; i < 30; i++) { //Exception starts appearing from this line.
        for (int j = 0; j < 6; j++) {
            subjects[i][j] = scan.nextInt();
        }
    }
    for (int i = 0; i < 30; i++) {
        avg1 = avg1 + (double) subjects[i][0];
        avg2 = avg2 + (double) subjects[i][1];
        avg3 = avg3 + (double) subjects[i][2];
        avg4 = avg4 + (double) subjects[i][3];
        avg5 = avg5 + (double) subjects[i][4];
        avg6 = avg6 + (double) subjects[i][5];
    }
    average[0] = avg1 / 30;
    average[1] = avg2 / 30;
    average[2] = avg3 / 30;
    average[3] = avg4 / 30;
    average[4] = avg5 / 30;
    average[5] = avg6 / 30;
    for (int i = 0; i < 30; i++) {
        if (subjects[i][0] >= 50) {
            num1++;
        }
        if (subjects[i][1] >= 50) {
            num2++;
        }
        if (subjects[i][2] >= 50) {
            num3++;
        }
        if (subjects[i][3] >= 50) {
            num4++;
        }
        if (subjects[i][4] >= 50) {
            num5++;
        }
        if (subjects[i][5] >= 50) {
            num6++;
        }
    }
    number[0] = num1;
    number[1] = num2;
    number[2] = num3;
    number[3] = num4;
    number[4] = num5;
    number[5] = num6;
    scan = new Scanner(new FileInputStream(new File("file.txt")));
    for (int i = 0; i < 30; i++) {
        for (int j = 0; j < 6; j++) {
            x = x + scan.nextInt();
        }
        total[i] = x;
        x = 0;
    }
    for (int i = 0; i < 30; i++) {
        p.println("Total score of student #" + (i + 1) + ": " + total[i]);
    }
    for (int i = 0; i < 6; i++) {
        p.println("Number of passing student in subject#" + (i + 1) + ": " + number[i]);
    }
    for (int i = 0; i < 6; i++) {
        p.println("Average score in subject#" + (i + 1) + ": " + average[i]);
    }
    p.close();
}

正如我所说,我只是通过创建一个新的实例来重置扫描仪的指针。只需用文件路径替换“file.txt”。

答案 1 :(得分:0)

好吧,你完整地阅读了total的所有数字。但是,你没有什么可读但仍然尝试。因此,您无法阅读,因此0numbersubjects的所有单元格中都有average个。更重要的是,当您尝试重新填充total时,0覆盖了Class_scores.txt

所以......你必须关闭并重新打开File或首先阅读它并存储在我推荐的情况下。

一旦你阅读了整个sum of forces = mass * acceleration (or a(t+1) = Ftotal / m) and v(t+1) = v(t) + a(t+1) * dt ,你就无法从头开始阅读。