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

后端 未结 14 2295
你的背包
你的背包 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:37

    From javadocs

    next() Returns the next token if it matches the pattern constructed from the specified string. nextLine() Advances this scanner past the current line and returns the input that was skipped.

    Which one you choose depends which suits your needs best. If it were me reading a whole file I would go for nextLine until I had all the file.

提交回复
热议问题