0

I'm just curious:

Assume I set up a scanner as such.

Scanner sc = new Scanner (System.in)
int number = sc.nextInt();
String name = sc.nextLine(); 
System.out.println (number);
System.out.println (name);

What would happen would be that I wouldn't even get to input the name and all that would print would be the number. Conversely if I were to do:

Scanner sc = new Scanner (System.in)
int number = sc.nextInt();
String name = sc.next(); 
System.out.println (number);
System.out.println (name);

Then everything works swimmingly, however one can't use spaces in the string.

Why do strings being scanned act so funny after being followed by a number. If I were to just use 2 strings it wouldn't do what it seems to.

I know the work around is to put a blank line in between the two, but I would just like to know WHY this happens.

  • Aren't you confusing "space" with "new line" character? – zubergu Apr 02 '15 at 06:56
  • your confusion is might be with `next()` and `nextLine()`?? – Prashant Apr 02 '15 at 06:57
  • No, I'm not. If I wanted to write the string "This blows" using the second example all that would print would be This. – Uri D. Charles Apr 02 '15 at 06:59
  • This happens because of the regular expressions patterns the next() functions are coded to consume. – John Doe Apr 02 '15 at 07:17
  • Scanner are tokenizer based on regular expression, so a nextLine() looks for the next '\n' and treats everything that follows after the current point to the next occurrence of '\n' as a token and returns that back as a String, whereas a next() finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. Hence a string can't have an empty space in between. Check these links http://www.cs.utexas.edu/users/ndale/Scanner.html http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – John Doe Apr 02 '15 at 07:19

2 Answers2

3

sc.nextLine() reads the current line until it encounters an end of line character. If you call sc.nextLine() after a call to a Scanner method that reads a partial line (nextInt(), next(), etc...), it would return the end of the current line (which might be empty if all that is left of the current line is the new line character).

Therefore, after you read input from a partial line, if you want to read the next line of input, you must first call sc.nextLine() to move past the current line and only then assign sc.nextLine() to a variable to get the content of the next line.

Eran
  • 359,724
  • 45
  • 626
  • 694
  • I see! Interesting. Thanks for the clarification. I was just curious, I knew how to work around it, but didn't know why I had to. – Uri D. Charles Apr 02 '15 at 07:02
1

I completely agree with @Eran's answer, just wanted to point out that usually that kind of information is right there in the javadoc, you just need to read it.

For nextInt():

* Scans the next token of the input as an <tt>int</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid int value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.

As you can see it doesn't say anything about jumping to the next line, it stays at the same line.

For nextLine():

* 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.

So after calling nextInt() you are still on the same line, if there's nothing more then nextLine() will just print nothing and jump to the next line.

Mateusz Dymczyk
  • 14,171
  • 8
  • 55
  • 90