title: Counting number of times words show up in txt file Java
body: <p>This program is suppsoed to count the number of times each word shows up in a .txt file
  I'm on the right track because this program shows every word in the file in a text area but I need it to not repeat words and increment a  counter instead. Am I on the right track under the for each word:words statement? I know that I just need some more logic in this statement but am not sure how to implement it. I should probably add that I'm using orderedlinkedlists to put all the words in alphabetical order in the text area so they or in order next to eachother.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.io.*;
  import java.util.Scanner;
  
  
  public class Oct29 extends JPanel
  {
    private OrderedList&lt;String&gt; words;
    private String filename;
    private int width = 800;
    private int height = 600;
    private TextArea textarea;
  
    public Oct29()
    {
      Scanner scan;
      textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
      textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
      textarea.setPreferredSize(new Dimension(width,height));
      setPreferredSize(new Dimension(width,height));
      add(textarea);
      JFileChooser chooser = new JFileChooser("../Text");
      int returnvalue = chooser.showOpenDialog(null);
  
      if(returnvalue == JFileChooser.APPROVE_OPTION)
      {
        try
        {
          File file = chooser.getSelectedFile();
          filename = file.getName();
          System.err.println(filename);
          scan = new Scanner(file);
        }
        catch (IOException e)
        {
          System.err.println("IO EXCEPTION");
          return;
        }       
      }
      else
      {
        return;
      }
      words = new OrderedLinkedList&lt;String&gt;();
      while(scan.hasNext())
      {
        String token = scan.next().toLowerCase();
        token = token.replace(",","").replace(".","");
        words.add(token);
      }
      scan.close();
      textarea.append("    "+filename+" has wordcount: "+words.size()+
        "\n-------------------------\n\n");
  
       for(String word : words)
       {
         for( int i=0;i&lt;words.size(); i++)
         {
         int x =0;       // This does not work but I'm trying to find if I'm on the right track
         if(x &lt; words.size())  
          textarea.append(word+"\n"); // THIS BY ITESELF WITHOUT THE INNER FOR LOOP WORKS FINE FOR DISPLAYING THE WORDS IN A TEXT AREA.
         x++;
       }
       }
    }
  
    public static void main(String[] arg)
    {
      JFrame frame = new JFrame("Oct 29");
      frame.getContentPane().add(new Oct29());
      frame.pack();
      frame.setVisible(true);
    }
  }
  </code></pre>