Tag Archives: Scanner.next() Scanner.nextLine()

The difference between Scanner.next() and Scanner.nextLine()

1. The space, tab key and enter key encountered before the next() method encounters a valid character cannot be used as a terminator, the next() method will automatically remove it, only when the next() method encounters a valid character After that, the next() method treats the space key, Tab key, or Enter key entered later as a separator or terminator, so next() cannot get the string with spaces, only part of the string (space front).

2. The terminator of the nextLine() method is the Enter key, that is, the nextLine() method returns all the strings before the Enter key, so the nextLine() method can get the string with spaces.