What's the difference between next() and nextLine() methods from Scanner class?

后端 未结 14 2285
你的背包
你的背包 2020-11-21 05:32

What is the main difference between next() and nextLine()?
My main goal is to read the all text using a Scanner which may be \"con

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 06:22

    next() can read the input only till the space. It can't read two words separated by a 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().

提交回复
热议问题