I am writing a program that reads the text file and display the name and the grad of the first student and the average of the class. For the file given above, the result would be as follows: The first in the class is Ahmad Hamwi has 16.00 The average of the class is 12.25 This is thew text file i am trying to read from
public class GradesReader {
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
File grades = new File ("C:\\Users\\the_k\\OneDrive\\Documents\\NetBeansProjects\\GradesReader\\src\\gradesreader\\grades.txt");
Scanner scan = new Scanner(grades);
int lineNumber= 0;
double grade;
String StudentName;
double min = 999;
double max = -999;
int sum = 0;
int count=0;
while (scan.hasNextLine()) {
lineNumber++;
StudentName = scan.next();
grade = scan.nextDouble();
count++;
if(grade>max){
max=grade;
}
if(grade<min){
min=grade;
}
System.out.println("The first in class is" + StudentName + "has" + max );
double avg = sum /count;
System.out.println("The average of the class is " + avg);
}
}
This is the error I keep getting
> Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at gradesreader.GradesReader.main(GradesReader.java:37)
I have been trying for hours now. I know the error is in line 37. It has something to do maybe with the type. I tried int and float but same.