Java使用扫描仪输入键按下
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18281543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java using scanner enter key pressed
提问by H J
I am programming using Java.
I am trying write code which can recognize if the user presses the enter key in a console based program.
我正在使用 Java 编程。
我正在尝试编写可以识别用户是否在基于控制台的程序中按下 Enter 键的代码。
How can I do this using java. I have been told that this can be done using either Scanner or, buffered input reader. I do not understand(or know how to use) buffered input reader.
我怎样才能使用java做到这一点。有人告诉我,这可以使用扫描仪或缓冲输入阅读器来完成。我不明白(或不知道如何使用)缓冲输入阅读器。
I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn't work
我尝试使用扫描仪执行此操作,但是在按两次 Enter 后程序终止,并且它不起作用
Scanner readinput = new Scanner(System.in);
String enterkey = "Hola";
System.out.print(enterkey);
enterkey = readinput.nextLine();
System.out.print(enterkey);
if(enterkey == ""){
System.out.println("It works!");
Thanks
谢谢
-- edit --
the following code works using the equals
method for the string instead of ==
-- 编辑 -- 以下代码使用equals
字符串的方法而不是==
Scanner readinput = new Scanner(System.in);
String enterkey = "Hola";
System.out.print(enterkey);
enterkey = readinput.nextLine();
System.out.print(enterkey);
if(enterkey.equals("")){
System.out.println("It works!");
how can this be done, and what are the pros to doing this using the buffered input reader?
如何做到这一点,以及使用缓冲输入读取器执行此操作的优点是什么?
采纳答案by Caleb Brinkman
This works using java.util.Scanner and will take multiple "enter" keystrokes:
这使用 java.util.Scanner 工作并且需要多次“输入”键击:
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
while(readString!=null) {
System.out.println(readString);
if (readString.isEmpty()) {
System.out.println("Read Enter Key.");
}
if (scanner.hasNextLine()) {
readString = scanner.nextLine();
} else {
readString = null;
}
}
To break it down:
分解:
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
These lines initialize a new Scanner
that is reading from the standard input stream(the keyboard) and reads a single line from it.
这些行初始化一个Scanner
从标准输入流(键盘)中读取的 new并从中读取一行。
while(readString!=null) {
System.out.println(readString);
While the scanner is still returning non-null data, print each line to the screen.
当扫描仪仍在返回非空数据时,将每一行打印到屏幕上。
if (readString.isEmpty()) {
System.out.println("Read Enter Key.");
}
If the "enter" (or return, or whatever) key is supplied by the input, the nextLine()
method will return an empty string; by checking to see if the string is empty, we can determine whether that key was pressed. Here the text Read Enter Keyis printed, but you could perform whatever action you want here.
如果输入提供了“输入”(或返回或其他)键,则该nextLine()
方法将返回一个空字符串;通过检查字符串是否为空,我们可以确定该键是否被按下。此处打印文本Read Enter Key,但您可以在此处执行任何您想要的操作。
if (scanner.hasNextLine()) {
readString = scanner.nextLine();
} else {
readString = null;
}
Finally, after printing the content and/or doing something when the "enter" key is pressed, we check to see if the scanner has another line; for the standard input stream, this method will "block" until either the stream is closed, the execution of the program ends, or further input is supplied.
最后,在按下“回车”键时打印内容和/或执行某些操作后,我们检查扫描仪是否有另一行;对于标准输入流,此方法将“阻塞”,直到流关闭、程序执行结束或提供进一步输入。
回答by priya raj
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
Double d = scan.nextDouble();
String newStr = "";
Scanner charScanner = new Scanner( System.in ).useDelimiter( "(\b|\B)" ) ;
while( charScanner.hasNext() ) {
String c = charScanner.next();
if (c.equalsIgnoreCase("\r")) {
break;
}
else {
newStr += c;
}
}
System.out.println("String: " + newStr);
System.out.println("Int: " + i);
System.out.println("Double: " + d);
This code works fine
这段代码工作正常