import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

/**
 * Class that calculates various metrics on a text file.
 *   @author Dave Reed (modified by INSERT YOUR NAME)
 *   @version 3/15/2013
 */
public class FileStats {
    private ArrayList<String> words;

    /**
     * Constructs a FileStats object that reads in words from a file.
     *   @param fileName the file containing the words
     */
    public FileStats(String fileName) {
        this.readNewFile(fileName);
    }

    /**
     * Reads words from the specified file, overwriting any already stored.
     *   @param fileName the file containing the words
     */
    public void readNewFile(String fileName) {
        this.words = new ArrayList<String>();

        try {
            Scanner infile = new Scanner(new File(fileName));
            while (infile.hasNext()) {
                String nextWord = infile.next();
                nextWord = this.strip(nextWord.toLowerCase());
                if (nextWord.length() > 0) {
                    this.words.add(nextWord);
                }
            }
            infile.close();
        }
        catch (java.io.FileNotFoundException e) {
            System.out.println("No such file: " + fileName);
        }        
    }

    /**
     * Accessor method for the number of words currently stored.
     *   @return the number of words stored
     */
    public int numWords() {
        return this.words.size();
    }

    ///////////////////////////////////////////////////////////////////////////////

    /**
     * Strips the specified String of all characters except letters and digits.
     *   @param str the String to be stripped
     *   @return str with all characters except letters and digits removed
     */
    private String strip(String str) {
        String copy = "";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isLetterOrDigit(ch)) {
                copy += ch;
            }
        }
        return copy;
    }
}
