java.util.NoSuchElementException:找不到行
当我通过扫描仪读取文件时,我的程序出现运行时异常。
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Day1.ReadFile.read(ReadFile.java:49)
at Day1.ParseTree.main(ParseTree.java:17)
我的代码是:
while((str=sc.nextLine())!=null){
i=0;
if(str.equals("Locations"))
{
size=4;
t=3;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Professions"))
{
size=3;
t=2;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Individuals"))
{
size=4;
t=4;
str=sc.nextLine();
str=sc.nextLine();
}
int j=0;
String loc[]=new String[size];
while(j<size){
beg=0;
end=str.indexOf(',');
if(end!=-1){
tmp=str.substring(beg, end);
beg=end+2;
}
if(end==-1)
{
tmp=str.substring(beg);
}
if(beg<str.length())
str=str.substring(beg);
loc[i]=tmp;
i++;
if(i==size ){
if(t==3)
{
location.add(loc);
}
if(t==2)
{
profession.add(loc);
}
if(t==4)
{
individual.add(loc);
}
i=0;
}
j++;
System.out.print("\n");
}
I got an run time exception in my program while I am reading a file through a Scanner.
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Day1.ReadFile.read(ReadFile.java:49)
at Day1.ParseTree.main(ParseTree.java:17)
My code is:
while((str=sc.nextLine())!=null){
i=0;
if(str.equals("Locations"))
{
size=4;
t=3;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Professions"))
{
size=3;
t=2;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Individuals"))
{
size=4;
t=4;
str=sc.nextLine();
str=sc.nextLine();
}
int j=0;
String loc[]=new String[size];
while(j<size){
beg=0;
end=str.indexOf(',');
if(end!=-1){
tmp=str.substring(beg, end);
beg=end+2;
}
if(end==-1)
{
tmp=str.substring(beg);
}
if(beg<str.length())
str=str.substring(beg);
loc[i]=tmp;
i++;
if(i==size ){
if(t==3)
{
location.add(loc);
}
if(t==2)
{
profession.add(loc);
}
if(t==4)
{
individual.add(loc);
}
i=0;
}
j++;
System.out.print("\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
Scanner
时,您需要使用hasNextLine()
检查是否有下一行,以便循环成为
返回 null 的读取器
在
EOF
上 这段代码取决于输入的格式是否正确with
Scanner
you need to check if there is a next line withhasNextLine()
so the loop becomes
it's readers that return null on
EOF
ofcourse in this piece of code this is dependent on whether the input is properly formatted
我也遇到了这个问题。
就我而言,问题是我在其中一个函数中关闭了扫描仪。
上面的代码产生了相同的例外,解决方案是仅在主函数中关闭扫描仪一次。
I also encounter with that problem.
In my case the problem was that I closed the Scanner inside one of the funcs..
The code above made the same exeption, the solution was to close the scanner only once at the main.
您正在调用
nextLine()
,并且当没有行时它会抛出异常,正如 javadoc 所描述的那样。它永远不会返回null
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
You're calling
nextLine()
and it's throwing an exception when there's no line, exactly as the javadoc describes. It will never returnnull
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
无论出于何种原因,如果 Scanner 类遇到无法读取的特殊字符,它也会发出相同的异常。除了在每次调用
nextLine()
之前使用hasNextLine()
方法之外,请确保将正确的编码传递给Scanner
构造函数,例如:扫描仪扫描仪 = new Scanner(new FileInputStream(filePath), "UTF-8");
For whatever reason, the Scanner class also issues this same exception if it encounters special characters it cannot read. Beyond using the
hasNextLine()
method before each call tonextLine()
, make sure the correct encoding is passed to theScanner
constructor, e.g.:Scanner scanner = new Scanner(new FileInputStream(filePath), "UTF-8");
您真正的问题是您调用“sc.nextLine()”的次数比行数多。
例如,如果您只有十个输入行,那么您可以ONLY调用“sc.nextLine()”十次。
每次调用“sc.nextLine()”时,都会消耗一个输入行。如果您调用“sc.nextLine()”次数多于行数,您将遇到一个名为的异常。
如果您必须调用“sc.nextLine()” n 次,那么您必须至少 n 行。
尝试更改您的代码以将您调用“sc.nextLine()”的次数与行数相匹配,我保证您的问题将会得到解决。
Your real problem is that you are calling "sc.nextLine()" MORE TIMES than the number of lines.
For example, if you have only TEN input lines, then you can ONLY call "sc.nextLine()" TEN times.
Every time you call "sc.nextLine()", one input line will be consumed. If you call "sc.nextLine()" MORE TIMES than the number of lines, you will have an exception called
If you have to call "sc.nextLine()" n times, then you have to have at least n lines.
Try to change your code to match the number of times you call "sc.nextLine()" with the number of lines, and I guarantee that your problem will be solved.
需要使用top comment但也要注意nextLine()。要消除此错误,只需
从 while 循环内调用 Once
您正在使用 while 仅向前查看 1 行。然后使用 sc.nextLine() 读取您要求 while 循环向前查看的单行前面的 2 行。
还将多个 IF 语句更改为 IF, ELSE 以避免读取多行。
Need to use top comment but also pay attention to nextLine(). To eliminate this error only call
Once from inside your while loop
You are using while to look ahead only 1 line. Then using sc.nextLine() to read 2 lines ahead of the single line you asked the while loop to look ahead.
Also change the multiple IF statements to IF, ELSE to avoid reading more than one line also.
我遇到了这个问题,我的结构是:
1 - 系统
2 - 注册<-> 3 - 验证
我在这 3 个步骤中的每一个步骤中都关闭了扫描仪。我开始仅在系统中关闭扫描仪,问题就解决了。
I ran into this problem, my structure was:
1 - System
2 - Registration <-> 3 - validate
I was closing Scanner on each of the 3 steps. I started to close the Scanner only in system and it solved.