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

public class LetterFreq {
    private static final String LETTERS = "abcdefghijklmnopqrstuvwxyz";
	private ArrayList<Integer> counts;
	private int numLetters;

	public LetterFreq(String fileName) throws java.io.FileNotFoundException {
	    this.counts = new ArrayList<Integer>();
	    for (int i = 0; i < LetterFreq.LETTERS.length(); i++) {
	        this.counts.add(0);
	    }
	    this.numLetters = 0;
	    
	    Scanner infile = new Scanner(new File(fileName));
	    while (infile.hasNext()) {
	        String nextWord = infile.next();
	        for (int c = 0; c < nextWord.length(); c++) {
	            char ch = nextWord.charAt(c);
	            int index = LetterFreq.LETTERS.indexOf(Character.toLowerCase(ch));
	            if (index >= 0) {
	                this.counts.set(index, this.counts.get(index)+1);
	                this.numLetters++;
	            }
	        }
	    }
	}
	   
    public int getCount(char ch) {
	    int index = LetterFreq.LETTERS.indexOf(Character.toLowerCase(ch));
	    if (index >= 0) {
	        return this.counts.get(index);
	    }
	    else {
	        return 0;
	    }
	}
	
	public double getPercentage(char ch) {
	    int index = LetterFreq.LETTERS.indexOf(Character.toLowerCase(ch));
	    if (index >= 0 && this.numLetters > 0) {
	        double percent = 100.0*this.counts.get(index)/this.numLetters;
	        return Math.round(10.0*percent)/10.0;
	    }
	    else {
	        return 0.0;
	    }
	}	    
	    
	public void showCounts() {
	    for (int i = 0; i < LetterFreq.LETTERS.length(); i++) {
	        char ch = LetterFreq.LETTERS.charAt(i);
	        System.out.println(ch + ": " + this.getCount(ch) + "\t(" +
	                           this.getPercentage(ch) + "%)");
	    }
	}
}
