什么可能导致 NoSuchElementException?

What may cause NoSuchElementException?

我花了大约两个小时寻找解决方案,但一无所获。据我所知 .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 套接字) {

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;

不知道为什么,但您的应用程序无法从 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不需要的

问题的原因是 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);    
}