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

Write a program that will read a line of text that ends with a period, which ser

ID: 3806443 • Letter: W

Question

Write a program that will read a line of text that ends with a period, which serves as a sentinel value . Display all the letters that occur in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type  int of length 26, so that the ele- ment at index 0 contains the number of a’s, the element at index 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:Use one of the methods to UpperCase or to LowerCase in the wrapper class  Character , described in chapter 6. You will find it helpful to define a method that takes a character as an argument and returns an int  value that is the correct index for that character . For example, the argument 'a' results in 0 as the return value , the argument 'b' gives 1 as the return value , and so on. note that you can use a type cast, such as (int ) letter, to change a char to an int . Of course, this will not get the number you want, but if you subtract (int )'a', you will then get the right index.) Allow the user to repeat this task until the user says she or he is through.

SAMPLE RUN #4: java LetterFreq

Interactive Session Standard Input Standard Error (empty) Standard Output Hide Invisibles

Highlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAllShow Highlighted Only

Question:

I got the basic code correct, but I'm having trouble with all the prompts. I can't seem to get the output I need. What is your solution?

Explanation / Answer

public class LetterFreq {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);

       while (true) {
           int[] charArray = new int[26];
           String text = sc.nextLine().toLowerCase();

           if (text.equals("he is through.") || text.equals("she is through"))
               break;

           // to omit the "." at the end
           text = (text.substring(0, text.length() - 1));

           for (char c : text.toCharArray()) {
               if (isAlpha(c)) {
                   charArray[getIndex(c)] += 1;
               }
           }
           // diplay the Freq
           for (int i = 0; i < 26; i++) {
               if (charArray[i] == 0)
                   continue;
               System.out.println(
                       "The letter " + (char) (i + 'A') + " occurred " + charArray[i] + " times in the sentence.");
           }
       }
   }

   private static int getIndex(char c) {
       return (int) (c - 'a');
   }

   private static boolean isAlpha(char c) {
       return c >= 'a' && c <= 'z';
   }

}

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