title: Trying to get the number of words line by line from a txt file
body: <p>I'm trying to read create a basic class that reads a text file I made, and prints out the number of words on each specific line. </p>
  
  <p>The output I want is each integer to be printed on a new line. For example, if the first line had 10 words, second had 23, third had 24, I want:</p>
  
  <p>10</p>
  
  <p>23</p>
  
  <p>24</p>
  
  <p>But for some reason my code is giving me</p>
  
  <p>190</p>
  
  <p>0</p>
  
  <p>Which is the total number of words in the file and when it resets to 0. What am I doing wrong?</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  public class numWords {
      public static void main(String[] args) throws FileNotFoundException {
      File file = new File("words.txt");
  
      Scanner scanner = new Scanner(file);
      int numWords = 0;
  
      while (scanner.hasNextLine()) {
          while (scanner.hasNext()) {
              numWords++; //counts number of words
              scanner.next(); //goes to the next word if available
  
          }
          System.out.println(numWords); //prints the number of words
          numWords = 0;  //resets it to 0
          scanner.nextLine(); //goes to the next line
  
      }
      scanner.close();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have tried making an if statement saying if there aren't anymore lines, then I would use scanner.close();. </p>