title: Word Length Frequency
body: <p>I've created a Java program in Eclipse. The program counts the frequency of each word. For example if the user entered 'I went to the shop' the program would produce the output '1 1 1 2' that is 1 word of length 1 ('I') 1 word of length 2 ('to') 1 word of length 3 ('the') and 2 words of length 4 ('went' , 'shop'). </p>
  
  <p>I've created this program to read a string entered by the user but I'm wanting to adjust the code to read each line of a text file. Any help would be great. </p>
  
  <pre><code>import java.util.Scanner;
  
  public class WordLengthFrequency
  {
  
      public static void main(String[] args)
      {
          Scanner scan = new Scanner(System.in);
  
          while (true)
          {
              System.out.println("Enter text: ");
  
              String s;
              s = scan.nextLine();
              String input = s;
              String strippedInput = input.replaceAll("\\W", " ");
  
              System.out.println("" + strippedInput);
  
              String[] strings = strippedInput.split(" ");
              int[] counts = new int[6];
              int total = 0;
              for (String str : strings)
                  if (str.length() &lt; counts.length)
                      counts[str.length()] += 1;
              for (String s1 : strings)
                  total += s1.length();   
              for (int i = 1; i &lt; counts.length; i++){    
                  StringBuilder sb = new StringBuilder(i).append(i + " letter words: ");
                  for (int j = 1; j &lt;= counts[i]; j++) {
                      sb.append('*');
                      System.out.println(i + " letter words: " + counts[i]);
                      System.out.println(sb);
                      System.out.println(("mean lenght: ") + ((double) total / strings.length));
                  }
              }
         }
      }
  }
  </code></pre>