subject

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. save it to your directory and study it, then compile and run it to see how it works. in reading 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') for use as an index. no test is done to ensure that the characters are in fact letters.1. run countletters and enter a phrase, that is, more than one word with spaces or other punctuation in between. it should throw an , 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 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: put the body of the first for loop in a try. add a catch that catches the exception, but don't do anything with it. compile and run your program.2. now modify the body of the catch so that it prints a useful message (e. g., "not a letter") followed by the exception. compile and run the program. although it's useful to print the exception for debugging, when you're trying to smoothly handle a condition that you don't consider erroneous you often don't want to. in your print statement, replace the exception with the character that created the out of bounds index. run the program again; much nicer! // // countletters. java reads a words from the standard input and prints the number of// occurrences of each letter in that word.// import java. util. scanner; public class countletters{ public static void main(string[] args) { int[] counts = new int[26]; scanner scan = new scanner(system. in); //get word from user system. out. print("enter a single word (letters only, ): "); string word = scan. nextline(); //convert to all upper case word = word. touppercase(); //count frequency of each letter in string for (int i=0; i < word. length(); i++) counts[word. charat(i)-'a']++; //print frequencies system. out. println(); for (int i=0; i < counts. length; i++) if (counts [i] ! = 0) system. out. println((char)(i +'a') + ": " + counts[i]); }}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 17:30
Ou listened to a song on your computer. did you use hardware or software?
Answers: 2
question
Computers and Technology, 22.06.2019 20:00
How is the number 372 written when expanded out to place values in the base 8 (octal) number system? a. 2 x 4 + 3 x 2 + 4 x 1 b. 3 x 64 + 7 x 8 + 2 x 1 c. 3 x 8 + 7 x 7 + 2 x 6 d. 3 x 100 + 7 x 10 + 2 x 1
Answers: 1
question
Computers and Technology, 22.06.2019 22:30
Who needs to approve a change before it is initiated? (select two.) -change board -client or end user -ceo -personnel manager -project manager
Answers: 1
question
Computers and Technology, 22.06.2019 23:00
Which factor is the most important when choosing a website host? whether customers will make secure transactions the number of email accounts provided the purpose of the website the quality of the host control panel
Answers: 3
You know the right answer?
File countletters. java contains a program that reads a word from the user and prints the number of...
Questions
question
Mathematics, 28.01.2020 00:31
question
Mathematics, 28.01.2020 00:31
question
Mathematics, 28.01.2020 00:31
question
Mathematics, 28.01.2020 00:31
Questions on the website: 13722363