import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
 * CountWords.java
 *
 * Created on October 11, 2006, 4:57 PM
 *
 * COP 2210 Examples
 *
 */

/**
 *
 * @author BIll Kraynek
 */
public class CountWords {
    
    /** Creates a new instance of CountWords */
    public CountWords() throws Exception {
        String fileName = JOptionPane.showInputDialog("Enter\n file name","src/CountWords.java");
        Scanner fileScanner = new Scanner(new File(fileName));
        // set up delimiters to be anything except a letter
        fileScanner.useDelimiter("[^a-zA-Z]+");
        ArrayList<String> words = new ArrayList<String>();
        ArrayList<Integer> counts = new ArrayList<Integer>();
        while( fileScanner.hasNext() ) {
            String word = fileScanner.next();
            if( words.contains(word) ) {
                int wordIndex = words.indexOf(word);
                counts.set(wordIndex,counts.get(wordIndex) + 1);
            } else {
                words.add(word);
                counts.add(1);
            }// end if
        }// end while
        JTextArea outArea = new JTextArea(30,40);
        Font myFont = new Font("monospaced", Font.BOLD, 20);
        outArea.setFont(myFont);
        String out = "";
        for( int i = 0; i < words.size(); i++ ) {
            out += "Word " + words.get(i) + " appeared " + counts.get(i) + "  time(s)\n";
        }// end for
        outArea.setText(out);
        JOptionPane.showMessageDialog(null, new JScrollPane(outArea));
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        new CountWords();
    }
    
}

