java.util.NoSuchElementException:在 java.util.Scanner.nextLine(Scanner.java:1585) 中找不到任何行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17459050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:04:08  来源:igfitidea点击:

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

javaexceptionjava.util.scanner

提问by Korey Hinton

I'm prompting for user input over and over again until the user types exit in which case the data object will be set to null and it will leave the loop. For the most part this code works, but sometimes I get a NoSuchElementExceptionon the line: reader = new Scanner(System.in);. This tends to happen after a few loops and not the first time through. If I alternatively declare the reader variable within the scope of the while loop then it will fail on the second loop.

我一遍又一遍地提示用户输入,直到用户键入 exit 在这种情况下,数据对象将被设置为 null 并且它将离开循环。对于此代码工作的大部分,但有时我得到一个NoSuchElementException异常就行了:reader = new Scanner(System.in);。这往往会在几次循环后发生,而不是第一次通过。如果我在 while 循环的范围内另外声明 reader 变量,那么它将在第二个循环中失败。

Mostly Working (Sometimes throws exception)

主要工作(有时会抛出异常)

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

Alternate (throws exception on second loop)

替代(在第二个循环中抛出异常)

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

StackTrace

堆栈跟踪

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)

What am I missing?

我错过了什么?

回答by Ted Hopp

Allocate the Scannerat the start of your code, not each time through the loop:

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

The way you were doing it, each Scannerthat you create is left orphaned at the next loop iteration. None of them were being closed. The exception most likely arises when some internal resource is consumed from all those garbage Scannerobjects.

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