【问题标题】:java.util.NoSuchElementException: No line foundjava.util.NoSuchElementException:找不到行
【发布时间】:2011-11-04 18:21:09
【问题描述】:

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

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");
}

【问题讨论】:

  • 欢迎来到 SO。发布代码时,您应该注意对其进行格式化以提高可读性......即删除不必要的空白行,正确缩进。还将您的堆栈跟踪格式化为代码以使其可读。我为你修正了格式。
  • 另外,您遗漏了最重要的部分,即sc 的声明和打开方式。

标签: java java.util.scanner


【解决方案1】:

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

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

【讨论】:

    【解决方案2】:

    Scanner 需要检查下一行是否有hasNextLine()

    所以循环变成了

    while(sc.hasNextLine()){
        str=sc.nextLine();
        //...
    }
    

    EOF 上返回 null 的是读者

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

    【讨论】:

      【解决方案3】:

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

      Scanner scanner = new Scanner(new FileInputStream(filePath), "UTF-8");

      【讨论】:

        【解决方案4】:

        你真正的问题是你调用“sc.nextLine()”比行数更多次

        例如,如果您只有 行输入,那么您可以ONLY 调用“sc.nextLine()” 次。

        每次调用“sc.nextLine()”,都会消耗一个输入行。如果您调用“sc.nextLine()”比行数更多次您将有一个异常调用

              "java.util.NoSuchElementException: No line found".
        

        如果你必须调用 "sc.nextLine()" n 次,那么你必须有 至少 n 行。

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

        【讨论】:

          【解决方案5】:

          我也遇到过这个问题。 就我而言,问题是我关闭了其中一个功能内的扫描仪..

          public class Main 
          {
          	public static void main(String[] args) 
          	{
          		Scanner menu = new Scanner(System.in);
                  boolean exit = new Boolean(false);
              while(!exit){
          		String choose = menu.nextLine();
                  Part1 t=new Part1()
                  t.start();
          	    System.out.println("Noooooo Come back!!!"+choose);
          		}
          	menu.close();
          	}
          }
          
          public class Part1 extends Thread 
          {
          public void run()
            { 
               Scanner s = new Scanner(System.in);
               String st = s.nextLine();
               System.out.print("bllaaaaaaa\n"+st);
               s.close();
          	}
          }
          
          		 

          上面的代码做了同样的解释,解决办法是在主界面只关闭一次扫描仪。

          【讨论】:

          • 感谢那位哥们,坚持这个小错误
          • 我也有类似的问题。我想知道如果我关闭扫描仪并在阅读下一行时再次重新初始化它,为什么它不会工作。它们都应该是不同的扫描仪实例,对吗?为什么关闭一个扫描仪实例会影响另一个?
          【解决方案6】:

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

          sc.nextLine()
          

          从你的 while 循环中一次

           while (sc.hasNextLine()) {sc.nextLine()...}
          

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-08
            相关资源
            最近更新 更多