从控制台Java读取带有换行符的字符串
问题描述:
例如,我想读取此字符串(从控制台而不是文件):
I want to read this string (from console not file) for example:
one two three
four five six
seven eight nine
因此,我想每行读取一次并将每行放入数组中. 我该怎么读?因为如果使用扫描仪,我只能读取一行或一个单词(下一行或下一行).
So I want to read it per line and put every line in an array. How can I read it? Because if I use scanner, I can only read one line or one word (nextline or next).
例如,我的意思是阅读:one two trhee \n four five six \n seven eight nine...
what I mean is to read for example : one two trhee \n four five six \n seven eight nine...
答
您应该自己做! 有一个类似的例子:
You should do by yourself! There is a similer example:
public class ReadString {
public static void main (String[] args) {
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Thanks for the name, " + userName);
}
} // end of ReadString class