Keep getting NoSuchElementException using Scanner

十年热恋 提交于 2019-12-13 18:39:18

问题


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)

回答1:


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();
}



回答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();



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/33161051/keep-getting-nosuchelementexception-using-scanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!