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)