Java使用扫描仪输入键按下 [英] Java using scanner enter key pressed

查看:27
本文介绍了Java使用扫描仪输入键按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 进行编程.
我正在尝试编写可以识别用户是否在基于控制台的程序中按下回车键的代码.

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 来做到这一点.有人告诉我,这可以使用 Scanner 或缓冲输入阅读器来完成.我不了解(或不知道如何使用)缓冲输入阅读器.

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.

我尝试使用扫描仪执行此操作,但按两次回车后程序终止,并且不起作用

I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn't work

    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!");

谢谢

-- 编辑--以下代码使用字符串的 equals 方法而不是 ==

-- edit -- the following code works using the equals method for the string instead of ==

    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?

推荐答案

这使用 java.util.Scanner 并且需要多次输入"键击:

This works using java.util.Scanner and will take multiple "enter" keystrokes:

    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;
        }
    }

分解:

Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();

这些行初始化一个新的 Scanner,它从 标准输入流(键盘)并从中读取一行.

These lines initialize a new Scanner that is reading from the standard input stream (the keyboard) and reads a single line from it.

    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.");
        }

如果输入提供了enter"(或return,或其他)键,nextLine() 方法将返回一个空字符串;通过检查字符串是否为空,我们可以确定该键是否被按下.此处打印了 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 Key is printed, but you could perform whatever action you want here.

        if (scanner.hasNextLine()) {
            readString = scanner.nextLine();
        } else {
            readString = null;
        }

最后,在打印完内容和/或按下enter"键后,我们检查扫描仪是否有另一行;对于标准输入流,此方法将阻塞",直到流关闭、程序执行结束或提供进一步的输入.

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.

这篇关于Java使用扫描仪输入键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆