0

I don't know why I keep getting an InputMismatchException :/ It highlights when I call the load() method.

Thanks

import java.util.*;
import java.io.*;
public class GradeManager {
	
	private ArrayList <Student> StudentGrades;
	
	public GradeManager(){
		
		StudentGrades=new ArrayList<Student>();
		load();
		
	}
		
	public void load(){
		
		Scanner fileIn=null;
		try{
			
			fileIn=new Scanner(new File("grades.txt"));
		}catch(FileNotFoundException e){
			
			System.out.print("Cannot find file.");
			System.exit(-1);
		}
		
		while(fileIn.hasNextLine()){
			
			//fileIn.nextLine();
			StudentGrades.add(new Student(fileIn.nextLine(), fileIn.nextInt()));
		}
	}
	
	
	public static void main(String[] args){
		
		GradeManager g=new GradeManager();
		
	}

	
}

This is the format of the file:

Name
Grade(Integer)
...

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by kramerd
0

The error is probably because the call to nextInt on line 29 does not advance the read pointer to the next line. Try moving line 28 to after line 29 and take the comment marker off. If this doesn't fix the problem, please post your student class and attach the data file you're trying to read.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.