1

I have to complete a project that does the following: Write a program that prompts the user to input a string of words, then counts and displays the number of times each letter in the alphabet appears in the string. It is not necessary to distinguish between uppercase and lowercase letters. Your output should be formatted as follows:

Letter A count = xx
Letter B count = xx

....
Letter Z count = xx

I edited it so it looks like this now. The only issue now is that the uppercase letters are being ignored during the letter count, and I'm not quite sure what the issue is.

public class Assignment9 {

    public static void main(String[] sa) {

        int letters [] = new int[ 26 ];
        String s;
        char y;

        for ( int x = 0; x < letters.length; x++ )
        {
            letters[x] = 0;
        }

        s = Input.getString("Type a phrase with characters only, please.");
        s.toLowerCase();

        for ( int x = 0; x < s.length(); x++ )
        {
            y = s.charAt(x);
            if ( y >= 'a' && y <= 'z' )
            {
                letters[ y - 'a' ]++;   
            }

        }

        for ( y = 'a'; y <= 'z'; y++ )
        {
            System.out.println( "Letter " + y + " = " + letters[ y - 'a'] + " ");
        }

    }

}
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • 2
    Hint: you're currently showing the counts of all the letters *within the counting loop*. – Jon Skeet Jul 20 '17 at 19:47
  • print the counts after the counting part is done, youre printing the values while youre calculating them – ja08prat Jul 20 '17 at 19:49
  • Possible duplicate of [Java: How do I count the number of occurrences of a char in a String?](https://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) –  Jul 20 '17 at 20:18
  • @hwdbc It's not exactly the same question... – Valentin Genevrais Jul 20 '17 at 20:19
  • @ValentinGenevrais I suppose you're right. I retracted the flag. –  Jul 20 '17 at 20:24

2 Answers2

0

You should count the letters first and then display the result, so the following loop should be outside the loop where you are iterating through input string:

for ( y = 'a'; y <= 'z'; y++ )
{
    System.out.println( "Letter " + y + " = " + letters[ y - 'a'] + " ");
}  
0

I have a solution for you :

public static void main(String[] args) {

    //Init your String
    String str = "Your string here !";
    str = str.toLowerCase();

    //Create a table to store number of letters
    int letters [] = new int[ 26 ];

    //For each char in your string
    for(int i = 0; i < str.length(); i++){
        //Transphorm letter to is corect tab index
        int charCode = (int)str.charAt(i)-97;

        //Check if the char is a lettre
        if(charCode >= 0 && charCode < 26 ){
            //Count the letter
            letters[charCode]++;
        }
    }

    //Display the result
    for(int i = 0; i < 26; i ++){
        char letter = (char)(i+65);
        System.out.println("Letter " + letter + " count = " + letters[i]);
    }

}
Valentin Genevrais
  • 421
  • 1
  • 6
  • 21