读取文件时出现java.util.NoSuchElementException(扫描仪) [英] java.util.NoSuchElementException on reading a file (Scanner)

查看:114
本文介绍了读取文件时出现java.util.NoSuchElementException(扫描仪)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个单词的文本文件,尝试读取这些单词时出现以下错误:

I have a text file with 3 words on it, and on trying to read these words I get the following error:

Exception in thread "main" java.util.NoSuchElementException  
    at java.util.Scanner.throwFor(Unknown Source)  
    at java.util.Scanner.next(Unknown Source)  
    at search.search(search.java:121)  
    at main.main(main.java:38)  

下面是有问题的代码:

Scanner reader = new Scanner(path + client + "\\" + cat +"\\" + query + ".arch");  
while (reader.hasNext()){  
    String a = reader.next();  // line 121
    String b = reader.next();  
    String c = reader.next();  
    file = new File (path + client + "\\" + a +"\\" + b + ".arch");  
    print(file);  
}

下面是文件的内容:

po ref refc(末尾有新行)

po ref refc (with a new line at the end)

有什么作用? 我以前使用的是非常相似的系统,具有相同的文件,并且一切正常,甚至在下面的几行中都使用了非常相似的系统.
顺便说一句,字符串c尚未实现,但应该更进一步.

What gives? I've used a very similar system before, with the same file, and everything worked, I even use a very similar system some lines below.
Btw the String c is not yet implemented but shall be further down.

推荐答案

您确定第121行不是接下来的几行之一吗?

Are you sure line 121 is not one of the next lines?

您检查reader.hasNext()以确保还有另一个条目,但是随后在abc中读取了3个条目.例如,如果只有一个,则b = reader.next()将会失败.

You check reader.hasNext() to make sure there is another entry, but you then read 3 entries in a, b, c. If there is only one for example, b = reader.next() would fail.

这篇关于读取文件时出现java.util.NoSuchElementException(扫描仪)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆