How to capture enter key, using scanner as console input?

青春壹個敷衍的年華 提交于 2019-11-28 14:49:26

You can read line and if line is blank, You can assume it is enter key.. like below code..

Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
System.out.println(readString);
if (readString.equals(""))
    System.out.println("Enter Key pressed.");
if (scanner.hasNextLine())
    readString = scanner.nextLine();
else
    readString = null;

I think this will help you

Scanner input= new Scanner(System.in);
String readString = input.nextLine();
while(readString!=null) {
    System.out.println(readString);
    if (readString.equals(""))
        System.out.println("Read Enter Key.");
    if (input.hasNextLine())
        readString = input.nextLine();
    else
        readString = null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!