java.util.NoSuchElementException:找不到行 [英] java.util.NoSuchElementException: No line found
本文介绍了java.util.NoSuchElementException:找不到行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我通过扫描仪读取文件时,我的程序出现运行时异常.
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)
我的代码是:
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("
");
}
推荐答案
with Scanner
你需要检查下一行是否有 hasNextLine()
with Scanner
you need to check if there is a next line with hasNextLine()
所以循环变成
while(sc.hasNextLine()){
str=sc.nextLine();
//...
}
在 EOF
当然在这段代码中,这取决于输入的格式是否正确
ofcourse in this piece of code this is dependent on whether the input is properly formatted
这篇关于java.util.NoSuchElementException:找不到行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文