【问题标题】:Java Scanner Error : Exception in thread "main" java.util.NoSuchElementException: No line foundJava 扫描仪错误:线程“main”中的异常 java.util.NoSuchElementException:找不到行
【发布时间】:2013-09-10 17:57:24
【问题描述】:

我正在尝试获取用户输入,但我不断收到错误消息:找不到行 行号引用“input = fileS.nextLine();”作为错误的来源

System.out.print("Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N \n");
             Scanner fileS = new Scanner(System.in);

             input = fileS.nextLine();
             input = input.trim();
             input = input.toLowerCase();

             tableCount ++;
             fileS.close();

这是我的代码,我知道如果我使用 fileS.hasNextLine() 它将避免此错误 但 这会一起跳过用户输入。

我在这里错过了什么?

扫描仪处于公共功能中

提前致谢!

【问题讨论】:

  • 这段代码对我来说很好用。你能发布堆栈跟踪吗
  • 也许你的标准输入被重定向了。您使用的是哪个 IDE?您能否提供更多背景信息?
  • 如何跟踪堆栈?我正在使用 Eclipse 你还需要什么其他信息?
  • 线程“主”java.util.NoSuchElementException 中的异常:在 advMrkupEdit.tabler(advMrkupEdit.java:815) 的 java.util.Scanner.nextLine(Scanner.java:1585) 处找不到行错误是 815 行是我发布的代码,另一个错误是在 advMrkupEdit.main(advMrkupEdit.java:266) 处带有扫描仪的函数调用
  • 创建一个新的测试类,添加一个 main 方法并尝试从那里运行您的代码并告诉我们它是否在这个新上下文中工作。

标签: java


【解决方案1】:

你应该这样读取输入:

while(fileS.hasNextLine())
{
    //Your operations here
}

当你这样做时

fileS.nextLine();

它会尝试读取,如果没有找到行,它会抛出异常。还要找

fileS.close()

在你的代码文件中阅读。如果在读取操作之前关闭它,则不能再次打开它,它会抛出异常。我认为这是最可能的原因,因为我几周前就有了:P

【讨论】:

  • 非常正确,但正如我之前所说,我尝试了 while 循环,这很棒,因为它跳过了错误,但它也跳过了用户输入。再次纠正关闭输入流然后尝试重新打开它!但这不是这里的情况......在我上面提到的代码之前,我没有关闭输入流。我正在审查我的所有代码......此时我认为我有语法错误或其他一些代码不匹配错误。不过对于阅读本文的其他人来说,这是一个很好的答案,因为这很可能会解决他们的问题。
  • 也许寻找其他关闭扫描仪行。我的问题是因为我有两台扫描仪,并且在一种方法中我关闭了一台。当我在网上某处阅读时,关闭一个 InputStream(System.in) 将导致关闭程序执行中的所有 IS。
【解决方案2】:

好吧,你应该几乎总是使用hasNextLine() 而不是nextLine()。但是您正在跳过输入,因为您的System.in 可能没有指向console 或任何可以接受任何输入或打开inputstreaminputstream。这就是为什么它在hasNextLine 的情况下根本不起作用,而nextLine() 是一种强制读取方法,即使没有数据也是如此,它期待输入但无法获得它的抛出异常。也许你应该检查你的System.in 指向的位置。

您可以手动将其设置为setIn(Console()),也可以检查console()是否为非空。

【讨论】:

    【解决方案3】:

    您似乎在循环内调用 Scanner(System.in),尝试将其放在顶部或任何循环之前。这就是为什么测试有效而无效的原因。

    Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N 
    y
    Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N 
    Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Scanner.java:1585)
        at ScannerIssue.main(ScannerIssue.java:11)
    

    ..

            while(true){
            System.out.print("Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N \n");
            Scanner fileS = new Scanner(System.in);
    
            String input = fileS.nextLine();
            input = input.trim();
            input = input.toLowerCase();
    
    //        tableCount ++;
            fileS.close();
            }
    

    上面的代码导致错误 移动扫描仪文件S = new Scanner(System.in);到方法或类范围的开头。

    你可以这样使用:

    Scanner fileS = new Scanner(System.in); //moved out of the loop
                while (true) {
                    System.out
                            .print("Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N \n");
                    String input = fileS.nextLine();
                    input = input.trim();
                    input = input.toLowerCase();
                    // tableCount ++;
                    if ("n".equalsIgnoreCase(input)) {
                        break;
                    }
                }
                fileS.close(); //moved out of the loop
                System.out.println("Good bye!");
            }
    

    结果是:

    Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N 
    Y
    Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N 
    A
    Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N 
    N
    Good bye!
    

    【讨论】:

      【解决方案4】:

      如果它在您第一次循环时有效,则可能是以下问题。

      来自 Java 文档:

      当 Scanner 关闭时,如果源实现了 Closeable 接口,它将关闭其输入源。

      这意味着它关闭了 System.in

      删除fileS.close();,你的程序应该可以正常工作了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多