Reputation: 345
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)
Upvotes: 1
Views: 1500
Reputation: 494
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();
Upvotes: 2
Reputation: 770
Just like you have hasNextLine(), you should also use hasNext().
Upvotes: 0
Reputation: 201409
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();
}
Upvotes: 6
Reputation: 12383
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.
Upvotes: 2