Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

后端 未结 22 1100
北荒
北荒 2020-12-03 01:14

I\'m writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say

22条回答
  •  时光取名叫无心
    2020-12-03 01:52

    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.

    for reading the entire line you can use nextLine()

提交回复
热议问题