Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.util.NoSuchElementException:未找到任何行_Java_Java.util.scanner - Fatal编程技术网

java.util.NoSuchElementException:未找到任何行

java.util.NoSuchElementException:未找到任何行,java,java.util.scanner,Java,Java.util.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.nextLin

当我通过扫描仪读取文件时,程序中出现运行时异常

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");
}
while((str=sc.nextLine())!=null){
i=0;
如果(str.equals(“位置”))
{
尺寸=4;
t=3;
str=sc.nextLine();
str=sc.nextLine();
}
如果(str.equals(“专业”))
{
尺寸=3;
t=2;
str=sc.nextLine();
str=sc.nextLine();
}
如果(str.equals(“个人”))
{
尺寸=4;
t=4;
str=sc.nextLine();
str=sc.nextLine();
}
int j=0;
字符串loc[]=新字符串[大小];

当(j调用
nextLine()
时,它会在没有行时抛出异常,正如javadoc所描述的那样。它永远不会返回
null


使用
扫描仪
您需要使用
hasNextLine()

因此,循环变为

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}
是读卡器在
EOF


当然,在这段代码中,这取决于输入是否正确格式化

无论出于何种原因,如果遇到无法读取的特殊字符,Scanner类也会发出相同的异常。除了在每次调用
nextLine()之前使用
hasNextLine()
方法之外
,确保将正确的编码传递给
扫描仪
构造函数,例如:


Scanner Scanner=new Scanner(new FileInputStream(filePath),“UTF-8”);

您真正的问题是调用“sc.nextLine()”的次数超过了行数

例如,如果您只有10个输入行,那么您只能调用“sc.nextLine()”10次

每次调用“sc.nextLine()”,都会消耗一个输入行。如果调用“sc.nextLine()”的次数超过行数,则会出现一个名为

      "java.util.NoSuchElementException: No line found".
如果必须调用“sc.nextLine()”n次,则必须至少有n行


尝试更改代码,使调用“sc.nextLine()”的次数与行数相匹配,我保证您的问题会得到解决。

我也遇到了这个问题。 在我的情况下,问题是我关闭了其中一个funcs内的扫描仪

公共类主
{
公共静态void main(字符串[]args)
{
扫描仪菜单=新扫描仪(System.in);
布尔退出=新布尔值(false);
当(!退出){
字符串choose=menu.nextLine();
第1部分t=新的第1部分()
t、 start();
System.out.println(“nooooo回来!!!”+选择);
}
menu.close();
}
}
公共类第1部分扩展了线程
{
公开募捐
{ 
扫描仪s=新的扫描仪(System.in);
字符串st=s.nextLine();
系统输出打印(“bllaaaaa\n”+st);
s、 close();
}
}

需要使用顶部注释,但也要注意nextLine()。要消除此错误,只需调用

sc.nextLine()
从while循环内部执行一次

 while (sc.hasNextLine()) {sc.nextLine()...}
您正在使用while仅向前看1行。然后使用sc.nextLine()读取您要求while循环向前看的单行前面的2


另外,将多个IF语句更改为IF,ELSE,以避免同时读取多行。

欢迎这样做。发布代码时,您应该注意将其格式化以便于阅读…即删除不必要的空行,正确缩进。还可以将stacktrace格式化为代码以使其可读。我修复了表单为你装潢。另外,你遗漏了最重要的一点,那就是
sc
是如何声明和打开的。感谢那位朋友,我犯了这个小错误。我也有类似的问题。我想知道,如果我关闭扫描仪并在阅读下一行时重新初始化它,为什么它不起作用。它们都应该是扫描仪的不同实例ht?为什么关闭扫描仪的一个实例会影响另一个实例?