1

I'm trying to read in string values separated by whitespaces. Once I try to set them to a variable I get a NoSuchElementException Error. I've done similar things before where I had to it with integers instead and never got this error. Doing some research: java.util.NoSuchElementException: Read words from a file: It says that hasNext is implemented to work with next() while hasNextLine is implemented to work with nextLine(), so I tried replacing hasNextLine() with hasNext(), but still nothing. Can anyone help?

File fileName = new File("maze.txt");
Scanner file = new Scanner(fileName);

while(file.hasNextLine()){
    String line = file.nextLine();
    Scanner scanner = new Scanner(line);

    //error starts from here
    String room = scanner.next();
    String roomName = scanner.next();
    String wall1 = scanner.next();
    String wall2 = scanner.next();
    String wall3 = scanner.next();
    String wall4 = scanner.next();
    scanner.close();
}
file.close();

maze.txt

room 101 wall door0 wall wall
room 404 door0 wall door1 wall
room 420 wall wall wall door1
door door0 0 1 close
door door1 1 2 open

Error:

Exception in thread "main" java.util.NoSuchElementException
 at java.util.Scanner.throwFor(Unknown Source)
 at java.util.Scanner.next(Unknown Source)
 at maze.SimpleMazeGame.main(SimpleMazeGame.java:96)
Community
  • 1
  • 1
adventuredoge
  • 345
  • 3
  • 13

4 Answers4

6

You should be checking each next() with hasNext(). Also, I'd prefer to read the mazes.txt from the home folder and the try-with-resources Statement instead of a bare close() and to test for empty lines of input. You could do that with something like,

File fileName = new File(System.getProperty("user.home"), "maze.txt");
try (Scanner file = new Scanner(fileName)) {
  while (file.hasNextLine()) {
    String line = file.nextLine();
    if (line.isEmpty()) {
      continue;
    }
    Scanner scanner = new Scanner(line);
    // error starts from here
    String room = scanner.hasNext() ? scanner.next() : "";
    String roomName = scanner.hasNext() ? scanner.next() : "";
    String wall1 = scanner.hasNext() ? scanner.next() : "";
    String wall2 = scanner.hasNext() ? scanner.next() : "";
    String wall3 = scanner.hasNext() ? scanner.next() : "";
    String wall4 = scanner.hasNext() ? scanner.next() : "";
  }
} catch (Exception e) {
  e.printStackTrace();
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
2

The exception java.util.NoSuchElementException will be thrown if you call next() and there is nothing left to read.

It seems the last two lines in your file have 5 values instead of 6, and you are calling next() 6 times for each line.

eugenioy
  • 10,249
  • 16
  • 25
2

There is an issue with the data in the input file . Your logic expects six values in each line of input file (maze.text) but fouth line in maze.text has just five values . Thats why its failing . Remove the last two lines from maze.text , your existing code will work . Otherwise you need to put check just before you read into wall4. Something like this

String wall4;
if(scanner.hasNext())
wall4 = scanner.next();
KBR
  • 444
  • 1
  • 6
  • 24
0

Just like you have hasNextLine(), you should also use hasNext().

user2383728
  • 688
  • 1
  • 5
  • 13
  • Right. Wasn't trying to provide a complete solution. Just a suggestion that hasNext should be used along with hasNextLine. Guess should not post code that is not complete. – user2383728 Oct 16 '15 at 01:35