title: Count specific words from text file - Java
body: <p>I have a text file and I want to count the total number of specific words I have defined.</p>
  
  <p>My code:</p>
  
  <pre><code>    String word1 = "aa";
      String word2 = "bb";
  
      int wordCount = 0;
  
       //creating File instance to reference text file in Java
      File text = new File("D:/project/log.txt");
  
      //Creating Scanner instnace to read File in Java
      Scanner s = new Scanner(text);
  
      //Reading each line of file using Scanner class
      while (s.hasNext()) {
          totalCount++;
          if (s.next().equals(word1) || s.next().equals(word2)) wordCount++;
      }
  
      System.out.println("Word count:  " + wordCount);
  </code></pre>
  
  <p>However, it only counts the number of 'aa's. It doesn't count the number of 'bb's.
  what could be the problem?</p>