Prints the number of occurrences

Assignment Help Basic Computer Science
Reference no: EM131961528

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 ArrayIndexOutOfBoundsException, 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 ArrayIndexOutOfBoundsException if it occurs. Since you want don't want to go further when a non-letter occurs, the handler will be empty. Modify this method as follows:

§ Put the body of the first for loop in a try.

§ Add a catch that catches the exception, but don't go further 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, please): ");

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]);

}

}

Reference no: EM131961528

Questions Cloud

The promotion of eduss acquisition : Topics for Research and Analysis Promotional Goals: Identify specific objectives related to the promotion of EDUS's acquisition of the institution.
Describe how successful the author was in convincing you : Describe how successful the author was in convincing you to accept the validity of the "surprise ending" that was different from what you expected.
Explain person life in terms of nature-nurture influences : Explain the person's life in terms of nature/nurture influences. Provide an analysis of the role cognitive, physical, and social-emotional development/changes.
Prompts for and reads in a line of input : File ParseInts.java contains a program that does the following:
Prints the number of occurrences : 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.
Describe in detail political developments in russia : Describe in detail political developments in Russia from 1991 to present. Conclude with your opinion whether democracy is likely in Russia in the near future.
Write response on the given case : Provide a substantive contribution that advances the discussion in a meaningful way by identifying strengths of the posting, challenging assumptions.
What would you suggest to that company in order to improve : In the case of the Coyote and Road Runner, one might question the ethics of the two characters' behaviors on many fronts.
Difference between a class and an instance of a class : 1) What is the difference between a class and an instance of a class? Give an example.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Define a code generator for this ast structure

Define a code generator for this AST structure. (You may generate JVM code or code for any other computer architecture).

  Requirements and assumptions for new interface design

You are to prepare the project scope and initial requirements and assumptions for a new Interface Design project for the Museum Guided Exhibition as outlined in the Museum Guided Exhibition problem case study.

  Display a message that the magazine will be sent

display a message that the magazine will be sent.

  Educational or possibly technology related

Record at least a 1-2 minutes of audio. You can record yourself or someone else such as a short interview. Topic:Keep it educational or possibly technology related. Choose your own topic.

  It governance 3 to 4 paged1what is the intent of regulatory

3 to 4 paged ltbrgt ltbrgt1.what is the intent of regulatory compliance and what is the role of it in maintaining

  What analogy would you use to describe the interaction

What analogy would you use to describe the interaction? Describing how you think of these ideas might help one of your classmates.

  Describe the iterator interface and explain its advantages

What role do statistics gathered from the database play in query optimization?

  Why are the ambiguous cases in the polygonization algorithm

Design an implicit primitive function using the skeleton of your choice. The function must take as input a point and return an implicit value and also the gradient at that point.

  Continuum of very mature to cutting edge technology

How can the organization decide where along the continuum of very mature to cutting edge technology it belongs in its IT acquisition strategy?

  Determine an equation to model the orbit of the apollo 8

Determine an equation to model the orbit of the Apollo 8 command module if the radius of the moon is 1740 kilometers. Let the center of the moon be at the origin.

  What is the difference between zero copy

What is the difference between zero copy forwarding and kernel bypassing?

  E-business and e-commerce

Determine the fundamental business advantages and disadvantages of e-business and e-commerce across the business organization.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd