Java Beginner - Counting number of words in sentence

后端 未结 17 1673
忘了有多久
忘了有多久 2021-01-01 05:29

I am suppose to use methods in order to count number of words in the a sentence. I wrote this code and I am not quite sure why it doesn\'t work. No matter what I write, I on

17条回答
  •  迷失自我
    2021-01-01 05:57

    Actually, if you enter a sentence like:

    "Hello World"
    

    your code String sentence = in.next(); will only get the first word in the sentence i.e., Hello. So, you need to use in.nextLine() in place of in.next() to get the whole sentence i.e., Hello World.

提交回复
热议问题