Exception in thread “main” java.util.NoSuchElementException: No line found - Using scanner input [duplicate]

谁说胖子不能爱 提交于 2019-11-27 05:29:10

The problem is you are closing System.in (Scanner.close() closes the underlying stream). Once you do that, it stays closed, and is unavailable for input. You don't normally want to do that with standard input:

String searchedNode;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name you would like to remove from the list: ");
searchedNode = in.nextLine();
// in.close(); // <-- don't close standard input!

Also, for future reference, you should try to create more minimal test cases. It will help you debug and also remove a lot of noise from your questions. :-)

PM 77-1

As per How to solve java.util.NoSuchElementException in Java (as well as common sense) use the appropriate kind of hasNext method before each next.

In your particular case, it will be something like this:

if (in.hasNextLine) {
    in.nextLine();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!