Java 在while循环中获取用户输入

Java 在while循环中获取用户输入,java,input,java.util.scanner,Java,Input,Java.util.scanner,我试图通过使用Kryonet进行通信来创建一个基本的IRC。我遇到的问题是,在我的代码中,我不能安全地使用一个允许用户键入和发送消息的主while循环,因为Scanner给出了一个错误,似乎跳过了对nextLine()的调用。我要做的是让扫描仪等待用户输入,然后再继续 Scanner input = new Scanner(System.in); while (running){ System.out.print(":"); message.

我试图通过使用Kryonet进行通信来创建一个基本的IRC。我遇到的问题是,在我的代码中,我不能安全地使用一个允许用户键入和发送消息的主while循环,因为Scanner给出了一个错误,似乎跳过了对nextLine()的调用。我要做的是让扫描仪等待用户输入,然后再继续

    Scanner input = new Scanner(System.in);

    while (running){

        System.out.print(":");

        message.text = input.nextLine();

        client.sendTCP(message);

    }

    input.close();
更准确地说,程序将首先在行的开头添加“:”,然后在用户按enter键后获取用户键入的任何内容,然后将其发送到服务器。 下面是我得到的错误:

:Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at com.andrewlalisofficial.ChatClient.main(ChatClient.java:51)

这在这里起作用。

您正在关闭
系统。在
中(通过
扫描仪)
-不要这样做。如果您关闭它,然后尝试使用新的
扫描仪再次读取,它将抛出您发布的异常

Scanner input = new Scanner(System.in);
while (true){
    System.out.print(":");
    String text = input.nextLine();
    System.out.println(text);
}