Scanner is not reading the whole sentence sentence

前端 未结 2 1467
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 10:31

Scanner is not reading the whole sentence. Or let\'s say I\'m writing a method which reverses the words in a sentence while keeping them in their order in a sentence.

         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 11:08

    You use scan.next(). This reads until whitespace (or whatever delimiter you set earlier, which you haven't). The default delimiter is whitespace.

    Use scan.nextLine() to read an entire line of text (i.e. until you press enter).

    From the documentation:

    Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. [...]

    You can change the delimiter by using useDelimiter

    public Scanner useDelimiter(Pattern pattern)
    

    Sets this scanner's delimiting pattern to the specified pattern.

提交回复
热议问题