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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 17:00, silvijaaa
The two main ways in which marketers address the competition with their strategies are by satisfying a need better than a competition and by
Answers: 2
image
Computers and Technology, 23.06.2019 00:00, destinysmithds7790
Suppose you have 9 coins and one of them is heavier than others. other 8 coins weight equally. you are also given a balance. develop and algorithm to determine the heavy coin using only two measurements with the of the balance. clearly write your algorithm in the form of a pseudocode using the similar notation that we have used in the class to represent sorting algorithms
Answers: 1
image
Computers and Technology, 25.06.2019 01:30, 91miketaylor
The study of how to design software, solve problems such as computer security threats, or come up with better ways of handling data storage
Answers: 1
image
Computers and Technology, 25.06.2019 15:10, isabellamoneill
Who should have a career portfolio, and how often should it be updated? only artists need a career portfolio, which they should update after every gallery show. everyone should have a career portfolio, which they should update daily with their professional activities. all students should have a career portfolio, which they should update ever semester until they graduate. most professionals need a career portfolio, which they should update with regularly with important achievements.
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 in other subjects:

Konu
Social Studies, 28.01.2021 19:00
Konu
Mathematics, 28.01.2021 19:00
Konu
Mathematics, 28.01.2021 19:00
Konu
English, 28.01.2021 19:00
Konu
Mathematics, 28.01.2021 19:00
Konu
Mathematics, 28.01.2021 19:00