title: Average Word Length
body: <p>I am attempting to calculate the average word length of user input in Java in a very simplistic way. The actual "math" of the code I've already completed, and it seems to be working quite well, but there are some odd house keeping things I need to address in order to complete the code. </p>
  
  <p>So far, I have the following: </p>
  
  <pre><code>public class Main {
  
  public static void main(String[] args) {
  
      Scanner sc = new Scanner(System.in);
  
      System.out.println("Please type some words, then press enter: ");
  
      int count = 0;
      double sum = 0;
  
      while (sc.hasNext()) {
  
          String userInput = sc.next();
  
          double charNum = userInput.length();
          sum = charNum + sum;
          count++;
  
          double average = 0;
          if (count &gt; 0) {
              average = sum / count;
          }
  
  
          System.out.println("Average word length = " + average);
  
          }
      }
  }
  </code></pre>
  
  <p>The end result output should look like this: </p>
  
  <pre><code>run: 
  Please type some words, then press enter: 
  this is a test
  Average word length = 2.75
  BUILD SUCCESSFUL (total time: 10 seconds)
  </code></pre>
  
  <p>However, the output is looking like this: </p>
  
  <pre><code>run: 
  Please type some words, then press enter: 
  this is a test
  Average word length = 4.0
  Average word length = 3.0
  Average word length = 2.3333333333333335
  Average word length = 2.75
  </code></pre>
  
  <p>Based on the code that I've written, how can I change it so that: </p>
  
  <ul>
  <li>The "average word length" is only printed one final time. </li>
  <li>The program ends after the user presses enter </li>
  </ul>
  
  <p>Thank you for any suggestions. </p>