I am very close to the correct solution, but I can\'t seem to get there. Any sug
ID: 3807535 • Letter: I
Question
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 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.
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:
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
Who·ya·gonna·call?.
The·letter·A·occurred·3·times·in·the·sentence. The·letter·C·occurred·1·times·in·the·sentence. The·letter·G·occurred·1·times·in·the·sentence. The·letter·H·occurred·1·times·in·the·sentence. The·letter·L·occurred·2·times·in·the·sentence. The·letter·N·occurred·2·times·in·the·sentence. The·letter·O·occurred·2·times·in·the·sentence. The·letter·W·occurred·1·times·in·the·sentence. The·letter·Y·occurred·1·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
Well,·although·it·would·be·handy·if·the·fictional·Ghost-Busters·were·real·if·in·fact·spirits·were·real·but·as·the·latter·are·in·no·sense·real·then·it·is·okay·that·the·latter·isn't·as·well.
The·letter·A·occurred·14·times·in·the·sentence. The·letter·B·occurred·3·times·in·the·sentence. The·letter·C·occurred·2·times·in·the·sentence. The·letter·D·occurred·2·times·in·the·sentence. The·letter·E·occurred·20·times·in·the·sentence. The·letter·F·occurred·4·times·in·the·sentence. The·letter·G·occurred·2·times·in·the·sentence. The·letter·H·occurred·9·times·in·the·sentence. The·letter·I·occurred·12·times·in·the·sentence. The·letter·K·occurred·1·times·in·the·sentence. The·letter·L·occurred·12·times·in·the·sentence. The·letter·N·occurred·8·times·in·the·sentence. The·letter·O·occurred·6·times·in·the·sentence. The·letter·P·occurred·1·times·in·the·sentence. The·letter·R·occurred·10·times·in·the·sentence. The·letter·S·occurred·11·times·in·the·sentence. The·letter·T·occurred·20·times·in·the·sentence. The·letter·U·occurred·4·times·in·the·sentence. The·letter·W·occurred·5·times·in·the·sentence. The·letter·Y·occurred·2·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
I·wouldn't·be·to·sure·about·anything·because·basic·epistemology·states·we·cannot·know·anything·with·%100·certainty.
The·letter·A·occurred·8·times·in·the·sentence. The·letter·B·occurred·4·times·in·the·sentence. The·letter·C·occurred·4·times·in·the·sentence. The·letter·D·occurred·1·times·in·the·sentence. The·letter·E·occurred·9·times·in·the·sentence. The·letter·G·occurred·3·times·in·the·sentence. The·letter·H·occurred·3·times·in·the·sentence. The·letter·I·occurred·7·times·in·the·sentence. The·letter·K·occurred·1·times·in·the·sentence. The·letter·L·occurred·2·times·in·the·sentence. The·letter·M·occurred·1·times·in·the·sentence. The·letter·N·occurred·9·times·in·the·sentence. The·letter·O·occurred·7·times·in·the·sentence. The·letter·P·occurred·1·times·in·the·sentence. The·letter·R·occurred·2·times·in·the·sentence. The·letter·S·occurred·6·times·in·the·sentence. The·letter·T·occurred·12·times·in·the·sentence. The·letter·U·occurred·4·times·in·the·sentence. The·letter·W·occurred·4·times·in·the·sentence. The·letter·Y·occurred·4·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
If·only·we·could·all·just·get·along·but·if·we·could·what·would·be·the·price
Your Code's Actual Result:
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
Who·ya·gonna·call?.
The·letter·A·occurred·3·times·in·the·sentence. The·letter·C·occurred·1·times·in·the·sentence. The·letter·G·occurred·1·times·in·the·sentence. The·letter·H·occurred·1·times·in·the·sentence. The·letter·L·occurred·2·times·in·the·sentence. The·letter·N·occurred·2·times·in·the·sentence. The·letter·O·occurred·2·times·in·the·sentence. The·letter·W·occurred·1·times·in·the·sentence. The·letter·Y·occurred·1·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
Well,·although·it·would·be·handy·if·the·fictional·Ghost-Busters·were·real·if·in·fact·spirits·were·real·but·as·the·latter·are·in·no·sense·real·then·it·is·okay·that·the·latter·isn't·as·well.
The·letter·A·occurred·14·times·in·the·sentence. The·letter·B·occurred·3·times·in·the·sentence. The·letter·C·occurred·2·times·in·the·sentence. The·letter·D·occurred·2·times·in·the·sentence. The·letter·E·occurred·20·times·in·the·sentence. The·letter·F·occurred·4·times·in·the·sentence. The·letter·G·occurred·2·times·in·the·sentence. The·letter·H·occurred·9·times·in·the·sentence. The·letter·I·occurred·12·times·in·the·sentence. The·letter·K·occurred·1·times·in·the·sentence. The·letter·L·occurred·12·times·in·the·sentence. The·letter·N·occurred·8·times·in·the·sentence. The·letter·O·occurred·6·times·in·the·sentence. The·letter·P·occurred·1·times·in·the·sentence. The·letter·R·occurred·10·times·in·the·sentence. The·letter·S·occurred·11·times·in·the·sentence. The·letter·T·occurred·20·times·in·the·sentence. The·letter·U·occurred·4·times·in·the·sentence. The·letter·W·occurred·5·times·in·the·sentence. The·letter·Y·occurred·2·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
I·wouldn't·be·to·sure·about·anything·because·basic·epistemology·states·we·cannot·know·anything·with·%100·certainty.
The·letter·A·occurred·8·times·in·the·sentence. The·letter·B·occurred·4·times·in·the·sentence. The·letter·C·occurred·4·times·in·the·sentence. The·letter·D·occurred·1·times·in·the·sentence. The·letter·E·occurred·9·times·in·the·sentence. The·letter·G·occurred·3·times·in·the·sentence. The·letter·H·occurred·3·times·in·the·sentence. The·letter·I·occurred·7·times·in·the·sentence. The·letter·K·occurred·1·times·in·the·sentence. The·letter·L·occurred·2·times·in·the·sentence. The·letter·M·occurred·1·times·in·the·sentence. The·letter·N·occurred·9·times·in·the·sentence. The·letter·O·occurred·7·times·in·the·sentence. The·letter·P·occurred·1·times·in·the·sentence. The·letter·R·occurred·2·times·in·the·sentence. The·letter·S·occurred·6·times·in·the·sentence. The·letter·T·occurred·12·times·in·the·sentence. The·letter·U·occurred·4·times·in·the·sentence. The·letter·W·occurred·4·times·in·the·sentence. The·letter·Y·occurred·4·times·in·the·sentence.
Enter·a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end:
If·only·we·could·all·just·get·along·but·if·we·could·what·would·be·the·price
The·letter·A·occurred·3·times·in·the·sentence. The·letter·B·occurred·2·times·in·the·sentence. The·letter·C·occurred·3·times·in·the·sentence. The·letter·D·occurred·3·times·in·the·sentence. The·letter·E·occurred·5·times·in·the·sentence. The·letter·F·occurred·2·times·in·the·sentence. The·letter·G·occurred·2·times·in·the·sentence. The·letter·H·occurred·2·times·in·the·sentence. The·letter·I·occurred·3·times·in·the·sentence. The·letter·J·occurred·1·times·in·the·sentence. The·letter·L·occurred·7·times·in·the·sentence. The·letter·N·occurred·2·times·in·the·sentence. The·letter·O·occurred·5·times·in·the·sentence. The·letter·P·occurred·1·times·in·the·sentence. The·letter·R·occurred·1·times·in·the·sentence. The·letter·S·occurred·1·times·in·the·sentence. The·letter·T·occurred·5·times·in·the·sentence. The·letter·U·occurred·5·times·in·the·sentence. The·letter·W·occurred·4·times·in·the·sentence. The·letter·Y·occurred·1·times·in·the·sentence.
Explanation / Answer
Hi, Please find my working program.
Please let me know in case of any issue.
import java.util.Scanner;
public class Program {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String input = "";
int[] a=new int[26];//to count Number of Characters
System.out.print("Enter text: ");
input = sc.nextLine(); // reading user input
char[] c = input.toCharArray();//converting string to char[]
for(int i=0;i<c.length;i++){
if(Character.isDigit(c[i])) {
System.out.println();
System.exit(0);
}
if(Character.isLetter(c[i]))//to check letter is Character or not
{
if(Character.isLowerCase(c[i]))
c[i]=Character.toUpperCase(c[i]);
int index = c[i]-'A';//finding index
int b=a[index];
b++;
a[index]=b;//incrementing value in an array
}
}
for(int i=0;i<a.length;i++)
{
if(a[i] > 0){
System.out.printf(("The letter %c occurred ") + ("%d times in sentence"), i + 65 , a[i]);
System.out.println();
}
}
System.out.println();
}
}
/*
Sample run:
Enter text: If·only·we·could·all·just·get·along·but·if·we·could·what·would·be·the·price
Enter text: a·sentence·ending·with·a·period·or·anything·not·ending·in·a·period·to·end
The letter A occurred 4 times in sentence
The letter C occurred 1 times in sentence
The letter D occurred 5 times in sentence
The letter E occurred 8 times in sentence
The letter G occurred 3 times in sentence
The letter H occurred 2 times in sentence
The letter I occurred 7 times in sentence
The letter N occurred 11 times in sentence
The letter O occurred 5 times in sentence
The letter P occurred 2 times in sentence
The letter R occurred 3 times in sentence
The letter S occurred 1 times in sentence
The letter T occurred 5 times in sentence
The letter W occurred 1 times in sentence
The letter Y occurred 1 times in sentence
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.