what is the difference between sc.nextLine() and sc.next() in java 1

what is the difference between sc.nextLine() and sc.next() in java

Their differences are...

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.

Here is what the above code is Doing:
1. The first call to nextInt() reads the integer and leaves the newline character (i.e., \n) in the buffer.
2. The second call to nextLine() reads the newline character from the buffer and ignores it.
3. The third call to nextLine() reads the rest of the line and returns it.

Similar Posts