Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner issue when using nextLine after nextXXX [duplicate]

I am using the Scanner methods nextInt() and nextLine() for reading input.

It looks like this:

System.out.println("Enter numerical value");     int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st string");  String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println("Enter 2nd string"); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) 

The problem is that after entering the numerical value, the first input.nextLine() is skipped and the second input.nextLine() is executed, so that my output looks like this:

Enter numerical value 3   // This is my input Enter 1st string    // The program is supposed to stop here and wait for my input, but is skipped Enter 2nd string    // ...and this line is executed and waits for my input 

I tested my application and it looks like the problem lies in using input.nextInt(). If I delete it, then both string1 = input.nextLine() and string2 = input.nextLine() are executed as I want them to be.

like image 924
blekione Avatar asked Oct 27 '12 16:10

blekione


People also ask

Why is scanner skipping nextLine () after use of other next functions?

That's because the Scanner. nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner. nextLine returns after reading that newline.

Is scanner nextLine blocking?

However, if you construct a Scanner using an InputStream that may require waiting for data to arrive (e.g., reading text from a remote client, waiting for a page to load so you can read its source, etc.) then Scanner#nextLine() will block because it needs to wait for enough information to build the next line to arrive.

What is the difference between scanner next () and scanner nextLine ()?

next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input. nextLine() reads input including space between the words (that is, it reads till the end of line \n).

When would you use a scanner nextLine?

nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.


1 Answers

That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.

You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself).

Workaround:

  • Either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.nextFoo to consume rest of that line including newline

    int option = input.nextInt(); input.nextLine();  // Consume newline left-over String str1 = input.nextLine(); 
  • Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. For example, you may convert to an integer using Integer.parseInt(String) method.

    int option = 0; try {     option = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) {     e.printStackTrace(); } String str1 = input.nextLine(); 
like image 113
Rohit Jain Avatar answered Sep 19 '22 23:09

Rohit Jain