扫描器 HasNextLine 总是返回 false

use*_*482 5 java java.util.scanner

我正面临 Scanner.hasNextLine() 这个奇怪的问题。我不使用 Scanner 类,但我习惯使用 Scanner.hasNextLine() 作为循环的条件来连续获取用户的输入,但在这部分代码中它总是返回 false !

public static void main(String[] args) throws IOException {
    String s = "";
    Scanner ssc = new Scanner(System.in);
    Client cli = new Client();
    cli.HLO();
    Thread t = new Thread(new ThreadMessage(cli));//Thread that asks user for input using Scanner and closing it then
    t.start();
    boolean b = ssc.hasNextLine();
    while (true) {
        b = ssc.hasNextLine();
        System.out.println(b);
        if (b) {
            s = ssc.nextLine();
            System.out.println("you wrote" + s);
            cli.dp.setData(s.getBytes());
            cli.ds.send(cli.dp);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和输入一样,我只有一个带有单词 false 的行循环

有没有人知道它为什么会这样?

编辑:似乎错误来自使用另一个扫描仪的 HLO 函数,我删除了它,现在它的文字正确。

Shr*_*ari 2

尽量用.hasNext()方法而不是用.hasNextLine()方法。
您遇到了问题,因为.hasNextLine() 如果此扫描仪的输入中有另一行,该方法将返回 true。但.hasNext()如果此扫描器的输入中有另一个标记,则该方法返回 true。

请参阅:http ://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html