Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am very close to the correct solution, but I can't seem to get there. Any suggestions would be helpful. Thanks! Java: Write a program

I am very close to the correct solution, but I can't seem to get there. Any suggestions would be helpful.

Thanks!

Java:

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 as, the element at index 1 contains the number of bs, 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.

The solution I tried:

import java.util.Scanner; 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("I am done.")) break; text = (text.substring(0, text.length() - 1)); System.out.print("Enter a sentence ending with a period or anything not ending in a period to end:"); for(char c : text.toCharArray()) { if(isAlpha(c)) { charArray[getIndex(c)] += 1; } } 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'; } }

The failed test run:

CODELAB ANALYSIS: LOGICAL ERROR(S)

Problems Detected: The contents of your standard output is incorrect.

Given the following was entered from the keyboard: !!!!!If you built your letter counter properly it will not count anything but proper letters@@@@@@. AlsO The DegRee OF captilization SHOULD NOT AFFECT YOUR COUNTER in any way shape or form. Multiple sentences on one line are fine. As long as the line ends with a period. aaaaa bbbbb ttttt uuuuuuuuuu. Goodbye you displayed: ... Enter a sentence ending with a period or anything not ending in a period to end:The letter B occurred 1 times in the sentence. The letter D occurred 1 times in the sentence. The letter G occurred 1 times in the sentence. The letter O occurred 2 times in the sentence. The letter Y occurred 1 times in the sentence. instead of: ...

Failed 4 out of 4 test runs.

Failed Test Run #1

The contents of your standard output is incorrect. Interactive Session - WStandard Input - RStandard Output - WHide Invisibles

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

Expected Result:

 

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Whoyagonnacall?.

TheletterAoccurred3timesinthesentence. TheletterCoccurred1timesinthesentence. TheletterGoccurred1timesinthesentence. TheletterHoccurred1timesinthesentence. TheletterLoccurred2timesinthesentence. TheletterNoccurred2timesinthesentence. TheletterOoccurred2timesinthesentence. TheletterWoccurred1timesinthesentence. TheletterYoccurred1timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Well,althoughitwouldbehandyifthefictionalGhost-Busterswererealifinfactspiritswererealbutasthelatterareinnosenserealthenitisokaythatthelatterisn'taswell.

TheletterAoccurred14timesinthesentence. TheletterBoccurred3timesinthesentence. TheletterCoccurred2timesinthesentence. TheletterDoccurred2timesinthesentence. TheletterEoccurred20timesinthesentence. TheletterFoccurred4timesinthesentence. TheletterGoccurred2timesinthesentence. TheletterHoccurred9timesinthesentence. TheletterIoccurred12timesinthesentence. TheletterKoccurred1timesinthesentence. TheletterLoccurred12timesinthesentence. TheletterNoccurred8timesinthesentence. TheletterOoccurred6timesinthesentence. TheletterPoccurred1timesinthesentence. TheletterRoccurred10timesinthesentence. TheletterSoccurred11timesinthesentence. TheletterToccurred20timesinthesentence. TheletterUoccurred4timesinthesentence. TheletterWoccurred5timesinthesentence. TheletterYoccurred2timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Iwouldn'tbetosureaboutanythingbecausebasicepistemologystateswecannotknowanythingwith%100certainty.

TheletterAoccurred8timesinthesentence. TheletterBoccurred4timesinthesentence. TheletterCoccurred4timesinthesentence. TheletterDoccurred1timesinthesentence. TheletterEoccurred9timesinthesentence. TheletterGoccurred3timesinthesentence. TheletterHoccurred3timesinthesentence. TheletterIoccurred7timesinthesentence. TheletterKoccurred1timesinthesentence. TheletterLoccurred2timesinthesentence. TheletterMoccurred1timesinthesentence. TheletterNoccurred9timesinthesentence. TheletterOoccurred7timesinthesentence. TheletterPoccurred1timesinthesentence. TheletterRoccurred2timesinthesentence. TheletterSoccurred6timesinthesentence. TheletterToccurred12timesinthesentence. TheletterUoccurred4timesinthesentence. TheletterWoccurred4timesinthesentence. TheletterYoccurred4timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Ifonlywecouldalljustgetalongbutifwecouldwhatwouldbetheprice

Your Code's Actual Result:

 

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Whoyagonnacall?.

TheletterAoccurred3timesinthesentence. TheletterCoccurred1timesinthesentence. TheletterGoccurred1timesinthesentence. TheletterHoccurred1timesinthesentence. TheletterLoccurred2timesinthesentence. TheletterNoccurred2timesinthesentence. TheletterOoccurred2timesinthesentence. TheletterWoccurred1timesinthesentence. TheletterYoccurred1timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Well,althoughitwouldbehandyifthefictionalGhost-Busterswererealifinfactspiritswererealbutasthelatterareinnosenserealthenitisokaythatthelatterisn'taswell.

TheletterAoccurred14timesinthesentence. TheletterBoccurred3timesinthesentence. TheletterCoccurred2timesinthesentence. TheletterDoccurred2timesinthesentence. TheletterEoccurred20timesinthesentence. TheletterFoccurred4timesinthesentence. TheletterGoccurred2timesinthesentence. TheletterHoccurred9timesinthesentence. TheletterIoccurred12timesinthesentence. TheletterKoccurred1timesinthesentence. TheletterLoccurred12timesinthesentence. TheletterNoccurred8timesinthesentence. TheletterOoccurred6timesinthesentence. TheletterPoccurred1timesinthesentence. TheletterRoccurred10timesinthesentence. TheletterSoccurred11timesinthesentence. TheletterToccurred20timesinthesentence. TheletterUoccurred4timesinthesentence. TheletterWoccurred5timesinthesentence. TheletterYoccurred2timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Iwouldn'tbetosureaboutanythingbecausebasicepistemologystateswecannotknowanythingwith%100certainty.

TheletterAoccurred8timesinthesentence. TheletterBoccurred4timesinthesentence. TheletterCoccurred4timesinthesentence. TheletterDoccurred1timesinthesentence. TheletterEoccurred9timesinthesentence. TheletterGoccurred3timesinthesentence. TheletterHoccurred3timesinthesentence. TheletterIoccurred7timesinthesentence. TheletterKoccurred1timesinthesentence. TheletterLoccurred2timesinthesentence. TheletterMoccurred1timesinthesentence. TheletterNoccurred9timesinthesentence. TheletterOoccurred7timesinthesentence. TheletterPoccurred1timesinthesentence. TheletterRoccurred2timesinthesentence. TheletterSoccurred6timesinthesentence. TheletterToccurred12timesinthesentence. TheletterUoccurred4timesinthesentence. TheletterWoccurred4timesinthesentence. TheletterYoccurred4timesinthesentence.

Enterasentenceendingwithaperiodoranythingnotendinginaperiodtoend:

Ifonlywecouldalljustgetalongbutifwecouldwhatwouldbetheprice

TheletterAoccurred3timesinthesentence. TheletterBoccurred2timesinthesentence. TheletterCoccurred3timesinthesentence. TheletterDoccurred3timesinthesentence. TheletterEoccurred5timesinthesentence. TheletterFoccurred2timesinthesentence. TheletterGoccurred2timesinthesentence. TheletterHoccurred2timesinthesentence. TheletterIoccurred3timesinthesentence. TheletterJoccurred1timesinthesentence. TheletterLoccurred7timesinthesentence. TheletterNoccurred2timesinthesentence. TheletterOoccurred5timesinthesentence. TheletterPoccurred1timesinthesentence. TheletterRoccurred1timesinthesentence. TheletterSoccurred1timesinthesentence. TheletterToccurred5timesinthesentence. TheletterUoccurred5timesinthesentence. TheletterWoccurred4timesinthesentence. TheletterYoccurred1timesinthesentence.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

If you had a startup e - commerce company, would you use AWS?

Answered: 1 week ago

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago

Question

Consistently develop management talent.

Answered: 1 week ago

Question

Create a refreshed and common vision and values across Europe.

Answered: 1 week ago

Question

Provide the best employee relations environment.

Answered: 1 week ago