Я потратил около двух часов на поиск решения, но ничего не нашел. Насколько я знаю, .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;