Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java using scanner enter key pressed

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.

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

Thanks

-- 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?

like image 530
H J Avatar asked Aug 16 '13 20:08

H J


People also ask

How do you take input until Enter is pressed in Java?

Using an Extra nextLine() to Wait for Enter Key in Java Scanner scans the text and parse primitive types like int and String . This class comes with a lot of methods that are used in input operations. The most commonly used methods are the nextInt() , nextLine , nextDouble , and etc.

How do you represent enter in Java?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you enter a character in a Scanner class?

To take a char input using Scanner and next(), you can use these two lines of code. Scanner input = new Scanner (system.in); char a = input. next().


2 Answers

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

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.

    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 Key is printed, but you could perform whatever action you want here.

        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.

like image 149
Caleb Brinkman Avatar answered Sep 21 '22 18:09

Caleb Brinkman


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

like image 27
priya raj Avatar answered Sep 24 '22 18:09

priya raj