// **************************************************************** // CountLetters.java // // Reads a series of letters from the keyboard and prints the // number of occurrences of each letter. // // **************************************************************** import java.util.Scanner; public class CountLetters { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line; int[] counts = new int[26]; //get user input System.out.print("Enter a sequence of letters (enter an empty line to stop): "); line = scan.nextLine(); while (!line.equals("")) { //set all count array elements to 0 for (int i=0; i < counts.length; i++) counts[i] = 0; //convert to all upper case line = line.toUpperCase(); //counts the frequency of each letter in string for (int i=0; i < line.length(); i++) counts[line.charAt(i)-'A']++; //print frequencies System.out.println(); for (int i=0; i < counts.length; i++) if (counts [i] != 0) System.out.println((char)(i +'A') + ": " + counts[i]); //get user input System.out.print("\nEnter a sequence of letters (enter an empty line to stop): "); line = scan.nextLine(); } } }