MysteryProgram


Your browser is ignoring the <APPLET> tag!

This program counts the number or occurrences of each letter in a string. The output of this program goes into the browser's Java Console.

import objectdraw.*;

public class MysteryProgram extends WindowController
{
    private static final int NUM_LETTERS = 26;
    
    public void begin() {
        String saying = "FOOLS COUNT THEIR PROBLEMS.  WISE PEOPLE COUNT THEIR BLESSINGS.";
        int[] count = new int[NUM_LETTERS];
        char nextChar;
        
        for (int i = 0; i < saying.length(); i++) {
            nextChar = saying.charAt (i);
            if (nextChar >= 'A' && nextChar <= 'Z') {
                int charIndex = nextChar - 'A';
                count[charIndex]++;
            }
        }
        
        nextChar = 'A';
        for (int i = 0; i < NUM_LETTERS; i++) {
            System.out.println ("number of " + nextChar + " is " + count[i]);
            nextChar++;
        }
    }
}