什么可能导致 NoSuchElementException? [复制]
Posted
技术标签:
【中文标题】什么可能导致 NoSuchElementException? [复制]【英文标题】:What may cause NoSuchElementException? [duplicate] 【发布时间】:2020-05-08 21:07:59 【问题描述】:我花了大约两个小时寻找解决方案,但一无所获。据我所知.nextLine()
应该等待输入,而且通常会等待。但在这段代码中,它抛出了java.util.NoSuchElementException: No line found
。谁能解释可能导致此问题的原因?
请注意我是 java 新手。
这是一段令人不安的代码
private void interactiveMode() throws IOException
System.out.println("Launching remote collection manager");
try (Scanner userCommandReader = new Scanner(System.in))
System.out.println("Collection is ready for formatting");
System.out.println("Waiting for input. Type help for list of commands");
Thread.sleep(1000);
String testString = userCommandReader.nextLine();
System.out.println(testString);
catch (Exception e ) e.printStackTrace();
这里是完整的例外以防万一:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at client.ConnectToServer.interactiveMode(ConnectToServer.java:81) //line 81:String testString = userCommandReader.nextLine();
at client.ConnectToServer.server_tunnel(ConnectToServer.java:44)
at client.Main.main(Main.java:19)
这是调用其他两个方法的主要方法
System.out.println("Preparing Auth tool"); //Part of main method
boolean isAuthorized = Auth.serverAuth(outcoming); //when I'm skipping `serverAuth()`, interactiveMode works just fine.
System.out.println("isAuthorized="+isAuthorized);
if(isAuthorized) interactiveMode();
所以我的猜测是我在 serverAuth 方法中的某个地方有问题 public static Boolean serverAuth(Socket socket)
System.out.println("[WARNING] Launching Auth tool");
try (Scanner authScanner = new Scanner(System.in);
DataOutputStream authOut = new DataOutputStream(socket.getOutputStream());
DataInputStream authIn = new DataInputStream(socket.getInputStream());)
boolean loginStage=true;
boolean passwordStage=true;
while (loginStage)
System.out.println("Enter your login:");
String user_login=authScanner.nextLine();
authOut.writeUTF(user_login);
int serverLoginAnswer = authIn.read();
if (serverLoginAnswer == 1)
loginStage=false;
while (passwordStage)
System.out.println("Enter your password:");
String user_password = authScanner.nextLine();
String encrypted_user_password = ConnectToServer.encrypt_data(user_password);
authOut.writeUTF(encrypted_user_password);
int serverAuthAnswer = authIn.read();
if (serverAuthAnswer == 1)
passwordStage=false;
System.out.println("You were authorized");
return true;
else if (serverAuthAnswer == 0)
System.out.println("[!]Wrong password");
else if (serverLoginAnswer==0)
System.out.println("[!]Incorrect login");
catch (Exception e)
System.out.println("[!]Incorrect input");
e.printStackTrace();
return false;
【问题讨论】:
【参考方案1】:问题的原因是 Scanner
由于 try-with-resources
语法而关闭。请注意,一旦userCommandReader
关闭,System.in
也将关闭,无法再次打开。按如下方式进行:
private void interactiveMode() throws IOException
System.out.println("Launching remote collection manager");
Scanner userCommandReader = new Scanner(System.in)
System.out.println("Collection is ready for formatting");
System.out.println("Waiting for input. Type help for list of commands");
Thread.sleep(1000);
String testString = userCommandReader.nextLine();
System.out.println(testString);
【讨论】:
但是现在扫描仪没有关闭 @BorislavStoilov - 你绝不能关闭Scanner
以获取System.in
。正如我所提到的,一旦 System.in
的扫描仪关闭,System.in
也已关闭,无法再次打开。【参考方案2】:
不知道为什么,但您的应用程序要么无法从 std in 读取,要么读取速度很慢。
尝试等待换行,看看是否有什么不同。
System.out.println("Launching remote collection manager");
try (Scanner userCommandReader = new Scanner(System.in))
System.out.println("Collection is ready for formatting");
System.out.println("Waiting for input. Type help for list of commands");
while(userCommandReader.hasNextLine())
String testString = userCommandReader.nextLine();
System.out.println(testString);
catch (Exception e)
e.printStackTrace();
我删除了不需要的Thread.sleep
【讨论】:
谢谢你的回答,但不幸的是,它没有帮助我设法找到一个“可能的问题”我不知道如何在 cmets 中发布代码,所以我会放弃下一个回答这个问题没关系,我会把它添加到主要问题中以上是关于什么可能导致 NoSuchElementException? [复制]的主要内容,如果未能解决你的问题,请参考以下文章