A. Safwat A. Safwat - 1 month ago 5
Java Question

(Solved) Java: Getting zeroes in the output file

EDIT: I fixed the original Exception error thanks to Tim Biegeleisen by adding if(scan.hasNextInt()) in the lines that I put a comment on below. But the output file gives me zeroes instead of the expected results.

public static void main(String[] args) throws Exception {
File f = new File("Class_scores.txt");
Scanner scan = new Scanner(f);
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;
}

for (int i = 0; i < 30; i++) {
if(scan.hasNextInt()){ //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;
for (int i = 0; i < 30; i++) {
if(scan.hasNextInt()){ //this line
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();
}


The results in the output file:

Total score of student #1: 0
Total score of student #2: 0
Total score of student #3: 0
Total score of student #4: 0
Total score of student #5: 0
Total score of student #6: 0
Total score of student #7: 0
Total score of student #8: 0
Total score of student #9: 0
Total score of student #10: 0
Total score of student #11: 0
Total score of student #12: 0
Total score of student #13: 0
Total score of student #14: 0
Total score of student #15: 0
Total score of student #16: 0
Total score of student #17: 0
Total score of student #18: 0
Total score of student #19: 0
Total score of student #20: 0
Total score of student #21: 0
Total score of student #22: 0
Total score of student #23: 0
Total score of student #24: 0
Total score of student #25: 0
Total score of student #26: 0
Total score of student #27: 0
Total score of student #28: 0
Total score of student #29: 0
Total score of student #30: 0
Number of passing student in subject #1: 0
Number of passing student in subject #2: 0
Number of passing student in subject #3: 0
Number of passing student in subject #4: 0
Number of passing student in subject #5: 0
Number of passing student in subject #6: 0
Average score in subject #1: 0.0
Average score in subject #2: 0.0
Average score in subject #3: 0.0
Average score in subject #4: 0.0
Average score in subject #5: 0.0
Average score in subject #6: 0.0


I know that my code is very amateur-ish, but please bear with me as I'm still just a beginner. Thank you very much in advance!

EDIT2:
The text file that it takes input from is structured like this:

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: It seems like the problem is that it doesn't go beyond the if in the first place for some reason.

Answer

NoSuchElementException is thrown when the Scanner does not have (in this case) anymore int to read.

I don't know the structure of your .txt file so it would be great if you could add it to your question.

Probably the solution will be to reset the offset of the scanner by doing the following:

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();
    }
}

EDIT:

this code worked for me:

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();
}

As i said, i just reset the pointer of the Scanner by creating a new instance of it. Just replace the "file.txt" with your filepath.

Comments