Just started doing java programming. I searched stackoverflow and saw various solutions for this error but none of them solved the issue in my program. The program stops at "ENDDATA". I'm sure it is a simple fix that I can't seem to figure out:
Contents of student.dat file :
MARY 50 60 70 80
SHELLY 34 56 90 100
JOHN 32 54 66 88
ALFRED 21 100 88 75
ENDDATA
Program output:
The name of the student is MARY
His/her average score is 65
The name of the student is SHELLY
His/her average score is 70
The name of the student is JOHN
His/her average score is 60
The name of the student is ALFRED
His/her average score is 71
The name of the student is ENDDATA
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at Statistics.main(Statistics.java:32)
My code:
import java.util.*;
import java.io.*;
public class Statistics {
public static void main(String[] args)throws IOException {
Scanner in = new Scanner (new FileReader("c:\\students.dat"));
String name;
int nameCount = 0;
int avg = 0;
int spanish = 0;
int math = 0;
int french = 0;
int english = 0;
int highSpanish = 0;
int highMath = 0;
int highFrench = 0;
int highEnglish = 0;
int highAvg = 0;
name = in.next();
while (name!= "ENDDATA") {
nameCount++;
System.out.printf (" The name of the student is " + name + "\n");
name = in.next();
spanish = Integer.parseInt(name);
if (spanish > highSpanish) {
highSpanish = spanish;
}
name = in.next();
math = Integer.parseInt(name);
if (math > highMath) {
highMath = math;
}
name = in.next();
french = Integer.parseInt(name);
if (french > highFrench) {
highFrench = french;
}
name = in.next();
english = Integer.parseInt(name);
if (english > highEnglish) {
highEnglish = english;
}
avg = (spanish + math + french + english) /4;
if (avg > highAvg) {
highAvg = avg;
}
System.out.printf (" His/her average score is " + avg + "\n");
name = in.next();
}
System.out.printf (" The number of students in the class are " + nameCount);
System.out.printf (" The highest student average is " + highAvg);
System.out.printf (" The highest score for spanish is " + highSpanish);
System.out.printf (" The highest score for math is " + highMath);
System.out.printf (" The highest score for french is " + highFrench);
System.out.printf (" The highest score for english is " + highEnglish);
}
}