Java Beginner - Counting number of words in sentence

后端 未结 17 1674
忘了有多久
忘了有多久 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 06:08

    The simple logic is count the spaces only if there aren't any white spaces before. Try this:

    public class WordCount 
    {
        public static void main(String[] args) 
        {
            int word=1;
            Scanner s = new Scanner(System.in);
            System.out.println("Enter a string: ");
            String str=s.nextLine();
            for(int i=1;i
                                                            
提交回复
热议问题