Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File CountLetters.java contains a program that reads a word from the user and pr

ID: 3778672 • Letter: F

Question

File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. In reviewing the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0..25 (by subtracting 'A' or decimal 65) for use as an index. No test is done to ensure that the characters are in fact letters. 1. Add the proper comment section, run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArrayIndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter characters, but not count them. Of course, you could explicitly test the value of the character to see if it is between 'A' and 'Z'. However, an alternative is to go ahead and use the translated character as an index, and catch an ArrayIndexOutOfBoundsException if it occurs. Since you want don't want to do anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows: a. Put the body of the first for loop in a try. (5 points) b. Add a catch that catches the exception, but don't do anything with it. (5 points) 2. Now modify the body of the catch so that it prints a useful message (e.g., "Not a letter") followed by the exception. (5 points)

Explanation / Answer

import java.util.*;
//Count letters class definition
class CountLetters
{
   //Static counter for 26 alphabets
   static int count[] = new int[26];
   public static void main(String ss[])
   {
       //Scanner calss to accetp data
       Scanner sc = new Scanner(System.in);
       String ori= "", str;
       int c, no;
       do
       {
           //Accept a string
           str = sc.nextLine();
           //Stores the string if it is not end
           if(!str.equalsIgnoreCase("end"))
               ori += str + " " ;
           else
               break;
       }while(true);
       //Convert the string to upper case
       str = "";
       for(c = 0; c < ori.length(); c++)
       {
           if(ori.charAt(c) > 96 && ori.charAt(c) < 123 )
               str = str + (char)((int)ori.charAt(c) - 32);
           else
               str = str + ori.charAt(c);
       }
       System.out.println("Original String = " + str);
       //Exception handling
       try
       {
           //Loops till end of the string
           for(c = 0; c < str.length(); c++)
           {
               //Exctracts a character and converts it into integer
               no = (int) str.charAt(c);
               //Checks it for capital A to Z ascii value
               if (no >= 65 && no <= 90)
               {
                   //Decides the index position by taking the reminder no % 65 and count is increased
                   count[no % 65]++;
               }
           }
       }
       catch(ArrayIndexOutOfBoundsException a)
       {
           System.out.println("Not a alphabet");  
       }
       System.out.println("Number of occurrences of each letter in the word");  
       for(c = 0; c < 26; c ++)
       {
           //If the count position is not zero then display the character occurrence
           if(count[c]!= 0)
               System.out.println((char) (c + 65) + " Occurs = " + count[c] + " times");
       }
   }
}

Output:

This is 22
Try this
end
Original String = THIS IS 22 TRY THIS
Number of occurrences of each letter in the word
H Occurs = 2 times
I Occurs = 3 times
R Occurs = 1 times
S Occurs = 3 times
T Occurs = 3 times
Y Occurs = 1 times

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote