Scanner's Read Line returning NoSuchElementException

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2014-08-22T09:52:21Z Indexed on 2014/08/22 10:20 UTC
Read the original article Hit count: 181

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about io