Scanner doesn't see after space
Change to String name = scanner.nextLine();
instead of String name = scanner.next();
See more on documentation here - next() and nextLine()
Try replacing your code
String name = scanner.nextLine();
instead
String name = scanner.next();
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
). Once the input is read, nextLine()
positions the cursor in the next line.
From Scanner
documentation:
A
Scanner
breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
and
public String next()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
This means by default the delimiter pattern is "whitespace". This splits your text at the space. Use nextLine()
to get the whole line.