存在行我有我在哪里使用扫描仪类和循环线一码,直到没有剩下线。java.util.NoSuchElementException:没有找到线时文件
我的代码看起来是这样的:
File file = new File(filePath);
if (file.exists()) {
Scanner s = new Scanner(file);
String tmp = null;
int result = 0;
try {
while (true) {
tmp = s.nextLine();
if (tmp != null && tmp.equals("")) {
result += Integer.parseInt(tmp);
}
System.out.println(runSequence(Integer.parseInt(tokens[0])));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
}
它给在
TMP = s.nextLine)的误差(;
java.util.NoSuchElementException:没有找到行
这是奇怪的,因为前面相同的代码工作正常。
这条线为什么会出现错误?
编辑:
我的错误,我没有正确地说明这个问题,我特别离开try catch块了while循环,这样我可以做一个出口,当行结束...我的问题是,为什么我不是能够读取任何行...我有大约3-4线路中的TXT文件阅读和它没有任何阅读,并在第一线给予例外阅读本身......
这是一个依赖问题... –
您是否尝试过阅读的JavaDoc? http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine() – Keppil
@JacoVanNiekerk:在我应该怎么办?... –