A program to count frequencies of letters. Important: The program uses "new" Java formatting available in Java 1.5 (a.k.a. Java 5.0) only. You need to either install the new version of Java, or change 'printf' to earlier java formatting of doubles.

// CSci 4509 Computing letter frequencies
// Elena Machkasova, 1/26/05

import java.io.*;

public class LetterFrequency {
    public static void main(String[] args) throws IOException  {
	int [] letters = new int[26];
	int total = 0;

	// check if the file name has been supplied
	if (args.length < 1) {
	    System.out.println("You need to specify the file name");
	}

	// open the file
	FileReader in = new FileReader(new File(args[0]));
	
	int next;

	while ((next = in.read()) != -1) {
	    char c =(char) next;
	    if (Character.isUpperCase(c)) {
		letters[c - 'A']++;
		total++;
	    } else if (Character.isLowerCase(c)) {
		letters[c - 'a']++;
		total++;
	    }
	}

	for (int i = 0; i < 26; ++i) {
	    char c = (char) ('A' + i);
	    System.out.print(c + " " + letters[i] + "\t");
	    // C style formatting. Avaliable in Java 5.0. 
	    // Most innovations are well-forgotten old things :-)
	    System.out.printf("%5.3f\n", ((double) letters[i])/total);
	}

	in.close();
    }

}


The course main page.