我和STDIN有问题
例如,我将读取以下两个字符串:
输入:
abc
xyz
输入" abc ",然后按Enter键,我得到abc返回。但是我不想那样。我想键入另一个字符串,就像上面的输入一样。
所以想要的是:输入abc,输入,输入xyz
这是我的代码:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() != 0){
System.out.println(s);
}谢谢
发布于 2015-05-18 20:25:27
你应该用扫描仪做这个。
下面是一个实现扫描器的示例:
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String s2 = scanner.nextLine();
System.out.println(s + ":" + s2);
//Close scanner when finished with it:
scanner.close();下面是进一步阅读和示例的完整文档:Oracle文档
发布于 2015-05-18 20:30:39
扫描器是从控制台获取输入的首选方法。示例:
Scanner in = new Scanner(System.in);
System.out.print("Please enter a string: ");
String input = in.nextLine();
System.out.println("You entered: \"" + input + "\"");扫描仪还有其他有用的方法,如nextInt和nextChar。扫描仪上的完整文档
https://stackoverflow.com/questions/30312167
复制相似问题