0
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
    String a = in.nextLine();
    System.out.println(a + " 1");
}

I just wanna check what will happen when input is SPACE or ENTER, but it doesn't print anything until my input is neither SPACE nor ENTER, like this

a                     // input 
a 1                   // output 
                      // SPACE(input) 
c                     // input
  1                   // output
c 1                   // output

Why doesn't it print until it reads something which is neither BLANK nor SPACE? also, when it finally print, it print the SPACE which is the the line before c 1, when i input c, it give me 1 and c 1.

Nerooo
  • 3
  • 2
  • 1
    Have you read the documentation of `hasNext()`? What does it wait for? – Tom Sep 09 '20 at 15:19
  • Or here: https://stackoverflow.com/questions/47971568/can-scanner-next-return-null-or-empty-string – Savior Sep 09 '20 at 15:34
  • In your own words, why *should* it print immediately when a space is typed? What do you think `hasNext` is doing? Now, check the [documentation](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext()). Does it meet your expectation? What do you think a `token` is? What do you think `This method may block while...` means in this context? – Karl Knechtel Sep 09 '20 at 16:02

2 Answers2

0

Java Scanner hasNext() vs. hasNextLine():

That is, hasNext() checks the input and returns true if it has another non-whitespace character.
Whitespace includes not only the space character, but also tab space (\t), line feed (\n), and even more characters.
Continuous whitespace characters are treated as a single delimiter.

System.in means Standard Input.
When you pass System.in to init the Scanner, it will read data from Standard Input.
And it will always waiting for the input unless your input is EOF(Ctrl + Z in Windows or Ctrl + D).
So the scanner will always waiting for a non-whitespace character from input.

Input

When you press Space Enter, it sends two whitespace character \n to the Standard Input, and the function scanner.hasNext() is still waiting for a non-whitespace character. And scanner.hasNext() doesn't return anything. That's why there is no output at this time.
Then you press c, it sends non-whitespace character c to the Standard Input.

Output

Now your Standard Input contains \n c, the third one is not a whitespace character.
Finally the function scanner.hasNext() returns true.
Then the scanner.nextLine() read a line till character \n: it will be (one character),
and program print 1.

Standard Input now becomes c, only one character,
which will cause scanner.hasNext() to return true again:
Scanner will read a line, which will be one character c,
and print c 1.

s3cret
  • 149
  • 1
  • 8
  • Thanks a lot, I did check the documentation as previous comments suggested, but i missed the point that u mentioned on the output section,"Standard Input now becomes c". i do make a stupid mistake. thanks again! – Nerooo Sep 09 '20 at 16:23
-1

As mentionned by @Savior from this post :

hasNext() checks to see if there is a parseable token in the buffer, as separated by the scanner's delimiter. Since the scanner's delimiter is whitespace, and the linePattern is also white space, it is possible for there to be a linePattern in the buffer but no parseable tokens.

Consider using hasNextLine()

public class Main {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String a = in.nextLine();
            System.out.println(a + " 1");
        }
    }
}
IQbrod
  • 1,590
  • 1
  • 2
  • 20