Scanner's Read Line returning NoSuchElementException
- by Brian
This is my first time using StackOverflow. I am trying to read a text file which consists of a single number one the first line.
try {
Scanner s = new Scanner(new File("HighScores.txt"));
int temp =Integer.parseInt(s.nextLine());
s.close();
return temp;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, I get an error:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at GameStart.getHighScore(GameStart.java:334)
at GameStart.init(GameStart.java:82)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I know that HighScores.txt is not empty, so why is this problem occuring? I tried using BufferedReader, and BufferReader.readLine() return null.