Counting the frequency of letters of the alphabet in some lines of text

Quang :

I tried this program but it only printed out the frequency of some letters. Could someone tell my what I did wrong? Any help would be greatly appreciated. Yours truly, Quang Pham

This is what I got in a run of the program. Welcome to the Letter Count program. Please enter some lines of text followed by a period and a return. It takes a some time to compute. . . * 1: Four score and seven years ago our forefathers * 2: brought forth upon this continent a new nation, * 3: conceived in liberty, and dedicated to the * 4: proposition that all men are created equal. Letter Frequency a 13 c 6 e 19 g 2 i 9 k 0 m 1 o 15 q 1 s 6 u 5 w 1 y 2

import java.util.Scanner ;
/**
 *      The Letter Counter program counts the frequency of the letters of the 
 * alphabet in some lines of text.  After a period and a return, the computer
 * displays the frequency.
 *
 * @author Quang Pham
 * @version Module 8, Homework Project 2, 4/1/20
 * 
 *    Algorithm:
 *    
 *    1. User enters multiple lines of text.
 *    2. The program will read in the lines of text and display a list of all the 
 *       letters that occur in the text, with the number of times the letter occurs.
 *    3. The last line of input should be ended with a period, which serves as a 
 *       sentinel value.
 *    
 *    Problem description:
 *         Write a program that will read in multiple lines of text from the user 
 *    and display a list of all the letters that occur in the text, along with the 
 *    number of times each letter occurs.
 *
 *         The last line of input from the user should end with a period, which will 
 *    serve as a sentinel value.  Once the last line has been entered, the counts
 *    for all letters entered by the user should be listed in alphabetical order as 
 *    they are output.  Use an array of base type int of length 28, so that each 
 *    indexed variable contains the count of how many letters there are.  Array 
 *    indexed variable 0 contains the number of a’s, array indexed variable 1 contains
 *    the number of b’s and so forth.  Allow both uppercase and lowercase letters as
 *    input, but treat uppercase and lowercase versions of the same letter as being equal.
 *
 *    Hints: You might find it helpful to define a "helper" method that takes a character
 *    as an argument and returns an int value that is the correct index for that character,
 *    such as ‘a’ returning 0, ‘b’ returning 1, and so forth.  Note that you can use a 
 *    typecast to change a char to an int, like (int) letter.  This will not get the 
 *    number you want, but if you subtract (int) 'a', you will then have the right index. 
 *    Allow the user to repeat this task until the user says she or he is finished.
 *
 *    A dialog may look something like the following
 *
 *    Enter several lines of text to analyze. (Copy and paste to save time.)  When done,
 *    end a line with a period and press return.
 *    1: Four score and seven years ago our forefathers 
 *    2: brought forth upon this continent a new nation, 
 *    3: conceived in liberty, and dedicated to the  
 *    4: proposition that all men are created equal.
 *
 *    Here's the counts of characters:
 *    a: 13
 *    b: 2
 *    c: 6
 *    d: 7
 *    e: 19
 *    f: 4
 *    g: 2
 *    h: 6
 *    i: 9
 *    l: 4
 *    m: 1
 *    n: 14
 *    o: 15
 *    p: 3
 *    q: 1
 *    r: 12
 *    s: 6
 *    t: 15
 *    u: 5
 *    v: 2
 *    w: 1
 *    y: 2
 *
 *         Again, you can submit a single class for this project which contains your main
 *    method and any helper methods where you feel they can be used.
 *
 *    Along with the file containing your program, submit three print screens or screen 
 *    snips, each with several lines of text entered by the user, and the count for each
 *    character (a-z).
 */
public class LetterCounter
{
    public static void main(String[] args) {
        int frequency = 0 ;
        char character = ' ' ;
        String linesOfText = " " ; 

        char[] alphabet = new char[28] ; // new alphabet array        
        for(char ch = 'a'; ch <= 'z'; ++ch)// fills alphabet array with the alphabet
        {
            alphabet[ch-'a']=ch ;
        } 

        System.out.println("Welcome to the Letter Count program.") ; 
        System.out.println("Please enter some lines of text followed by a period and a return.") ;
        Scanner keyboard = new Scanner(System.in) ;
        linesOfText = keyboard.nextLine() ;
        System.out.println("Letter          Frequency") ;
        for (int i = 0; i < alphabet.length; i++) 
        {   frequency = 0 ;
            for (int j = 0; j < linesOfText.length(); j++) {
                character = linesOfText.charAt(j) ;
                if (character == alphabet[i]) {
                    frequency++ ;
                }
            }

            System.out.println(alphabet[i] + "\t\t" + frequency) ;
            i++;
        }
    }
}
WJS :

You asked what you did wrong. Well you are very close and you only did two things incorrectly.

  1. This line char[] alphabet = new char[28] should be 26.
  2. Look at the following code from your program.
 for (int i = 0; i < alphabet.length; i++) 
        {   frequency = 0 ;
            for (int j = 0; j < linesOfText.length(); j++) {
                character = linesOfText.charAt(j) ;
                if (character == alphabet[i]) {
                    frequency++ ;
                }
            }

            System.out.println(alphabet[i] + "\t\t" + frequency) ;
            i++;
        }

In the first loop you increment i using i++ But you also increment it at the end after the print statement. The last one should be removed.

A few observations

Instead of searching for your character inside an inner loop, just use the character to index into your list and bump the count. For example:

int count[] = new int[26];

say an you find the letter c =='i'.

count[c - 'a']++; ///increments the count for letter i.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355507&siteId=1