逐行读取输入 [英] Read input line by line
本文介绍了逐行读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java中逐行读取输入?我搜索过,到目前为止我有这个:
How do I read input line by line in Java? I searched and so far I have this:
import java.util.Scanner;
public class MatrixReader {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
System.out.print(input.nextLine());
}
}
这个问题是它没有读取最后一行。所以如果我输入
The problem with this is that it doesn't read the last line. So if I input
10 5 4 20
11 6 55 3
9 33 27 16
其输出仅为
10 5 4 20 11 6 55 3
推荐答案
理想情况下,您应该添加最终的println(),因为默认情况下,System.out使用仅在发送换行符时刷新的PrintStream。请参阅何时/为何调用System.out.flush() Java
Ideally you should add a final println() because by default System.out uses a PrintStream that only flushes when a newline is sent. See When/why to call System.out.flush() in Java
while (input.hasNext()) {
System.out.print(input.nextLine());
}
System.out.println();
虽然您的问题可能还有其他原因。
Although there are possible other reasons for your issue.
这篇关于逐行读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文