java.util.NoSuchElementException:找不到行

Ash*_*ery 30 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) 
Run Code Online (Sandbox Code Playgroud)

我的代码是:

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");
}
Run Code Online (Sandbox Code Playgroud)

rat*_*eak 28

Scanner您需要检查,如果存在与下一行hasNextLine()

所以循环变成了

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}
Run Code Online (Sandbox Code Playgroud)

它的读者返回null EOF

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


Bri*_*ach 12

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

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html


小智 10

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

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

		 
Run Code Online (Sandbox Code Playgroud)

上面的代码做了同样的解释,解决方案是在 main 处只关闭一次扫描器。

  • 我有类似的问题。我想知道为什么当我关闭扫描仪并在阅读下一行时重新初始化它时它不起作用。它们应该都是扫描仪的不同实例,对吧?为什么关闭扫描仪的一个实例会影响另一个实例? (3认同)

Wil*_*Hou 8

你真正的问题是,你在呼唤“sc.nextLine()”多次比的行数。

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

每次调用“sc.nextLine()”时,都会消耗一个输入行。如果你叫“sc.nextLine()”多次比的行数,你将有一个名为例外

      "java.util.NoSuchElementException: No line found".
Run Code Online (Sandbox Code Playgroud)

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

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


Luí*_*usa 7

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

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