2 回答
TA贡献1818条经验 获得超3个赞
语句中没有break,while所以你进入无限循环。
我用一个简单的System.out.println. 看一下新的while条件,当接收到一个空字符串时它会退出while语句:
Scanner sc = new Scanner(System.in);
String line;
while (!(line = sc.nextLine()).isEmpty()) {
System.out.println("Received line : " + line);
//textEditor.addString(line);
}
sc.close();
System.out.println("The end");
TA贡献1773条经验 获得超3个赞
您可以使用 break 语句跳出循环:
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (!(line.isEmpty())){
textEditor.addString(line);
} else {
break;
}
}
textEditor.printAll();
(顺便说一句,不要关闭标准输出、标准错误或标准输入,即在 Java 中:System.out、System.err 和 System.in)
添加回答
举报
