java.util.NoSuchElementException : No line found at java. util.Scanner.nextLine(Scanner.java:1585)

标签 java exception java.util.scanner

我一遍又一遍地提示用户输入,直到用户键入退出,在这种情况下,数据对象将被设置为空,并且它将离开循环。在大多数情况下,此代码可以工作,但有时我会在行上收到 NoSuchElementException:reader = new Scanner(System.in);。这种情况往往发生在几次循环之后,而不是第一次。如果我在 while 循环的范围内声明 reader 变量,那么它将在第二个循环中失败。

大部分工作正常(有时会抛出异常)

int level = 1;  
Scanner reader;
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    reader = new Scanner(System.in);

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
}
reader.close();

替代(在第二个循环中引发异常)

int level = 1;  
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    Scanner reader = new Scanner(System.in);

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
    reader.close();
}

堆栈跟踪

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at org.functional.utils.Menu.run(ServerScripts.java:61)
at org.functional.utils.ServerScripts.main(ServerScripts.java:18)

我错过了什么?

最佳答案

在代码开头分配Scanner,而不是每次循环时分配:

int level = 1;  
Scanner reader = new Scanner(System.in);
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
}
reader.close();

按照您的做法,您创建的每个 Scanner 在下一个循环迭代中都会被孤立。他们都没有被关闭。当所有这些垃圾 Scanner 对象消耗一些内部资源时,最有可能出现异常。

关于java.util.NoSuchElementException : No line found at java. util.Scanner.nextLine(Scanner.java:1585),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459050/

相关文章:

java - 如何正确调用延迟处理程序方法?

python-2.7 - PyAudio 录音机脚本 IOError : [Errno Input overflowed] -9981

java - 解析变量和左侧赋值以及计算++ 标记时出错

java - 非黑色背景的吴氏抗锯齿线算法

java - LocalDate - 解析区分大小写

java - 如何设置Scala/Java工作流以通过Jumpserver远程部署jar和进行远程调试?

c# - 如何在 .NET 中抛出的异常的 StackTrace 中获取行号以显示

c# - 验证异常

java - 我无法加载我的文件?

Java分隔符跳过一个单词