title: Java word count program
body: <p>I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string, the result of word count show wrong results because I am counting words on the basis of spaces used. I need help if there is a solution in a way that no matter how many spaces are I still get the correct result. I am mentioning the code below.</p>
  
  <pre><code>public class CountWords 
  {
      public static void main (String[] args)
      {
  
              System.out.println("Simple Java Word Count Program");
  
              String str1 = "Today is Holdiay Day";
  
              int wordCount = 1;
  
              for (int i = 0; i &lt; str1.length(); i++) 
              {
                  if (str1.charAt(i) == ' ') 
                  {
                      wordCount++;
                  } 
              }
  
              System.out.println("Word count is = " + wordCount);
      }
  }
  </code></pre>