这是我的代码:
try {
scan_movie_theater = new Scanner(new FileInputStream(new File("d:/movietheater.txt")));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while((line = scan_movie_theater.next()) != null ){
i = scan_movie_theater.nextInt();
System.out.println(line);
System.out.println(i);
}
scan_movie_theater.close();
我想像一个字符串一个整数一样读取该文件。我可以做到这一点。但是当我完成文件的读取时。我看到一个 java java.util.NoSuchElementException。我该如何解决这个问题?
这是我的 txt 文件。
dsa 435
salon 123
xxxx 123214324
最佳答案
请参阅 Javadoc。 Scanner.next() 不返回 null。在调用 next() 之前,您必须检查 hasNext()。
95% 的代码与问题无关,包括所有 Swing 代码。
关于java - 如何解决java.util.NoSuchElementException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23951763/