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

public class WordFreq1 {
    private ArrayList<String> words;
	private ArrayList<Integer> counts;
	private int totalWords;

	public WordFreq1(String fileName) throws java.io.FileNotFoundException {
	    this.words = new ArrayList<String>();
	    this.counts = new ArrayList<Integer>();
	    this.totalWords = 0;
	    
	    try {
	        Scanner infile = new Scanner(new File(fileName));
	        while (infile.hasNext()) {
	            String nextWord = infile.next().toLowerCase();
	            int index = words.indexOf(nextWord);
	            if (index >= 0) {
	                this.counts.set(index, this.counts.get(index)+1);
	            }
	            else {
	                this.words.add(nextWord);
	                this.counts.add(1);
	            }
	            this.totalWords++;
	        }
	    }
	    catch (java.io.FileNotFoundException e) {
	        System.out.println("FILE NOT FOUND: " + fileName);
	    }
	}
	   
    public int getCount(String str) {
	    int index = this.words.indexOf(str.toLowerCase());
        if (index >= 0) {
	        return this.counts.get(index);
	    }
	    else {
	        return 0;
	    }
	}
	
	public double getPercentage(String str) {
	    int index = this.words.indexOf(str.toLowerCase()); 
	    if (index >= 0) {
	        return 100.0*this.counts.get(index)/this.totalWords;
	    }
	    else {
	        return 0.0;
	    }
	}	    
	    
	public void showCounts() {
	    for (String nextWord : this.words) {
	        System.out.format("%-15s %5d (%5.1f%%)%n", nextWord+":", this.getCount(nextWord),
	                                                this.getPercentage(nextWord));
	    }
	}
}