User and prints the number of occurrences of each letter

Assignment Help JAVA Programming
Reference no: EM13884715

Exceptions Aren't Always Errors

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. In reviewing 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' or decimal 65) 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 do anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows:

Put a try/catch inside the first for loop.

Add a catch that catches the eception, but don't do anything with it.

  1. Now modify the body of the catch so that it displays a useful message followed by the character that is not a letter:

Not a letter: $

 

// ****************************************************************************

// CountLetters.java

// ****************************************************************************

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("n   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 < word.length(); i++)

         counts[word.charAt(i)-'A']++;

 

      //print frequencies

      System.out.println();

      for (int ; i < counts.length; i++)

         if (counts [i] != 0)

         System.out.println((char)(i +'A') + ": " + counts[i]);

 

   }

}

Reference no: EM13884715

Questions Cloud

Sensitive pressure-measuring device : A Small reaction bomb fitted with a sensitive pressure-measuring device is flushed out andfilled with pure reactant A at 1atm. Pressure. The operation is carried out at 25oC,atemperature low enough that the reaction does not proceed to any appreci..
User and prints the number of occurrences of each letter : 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. In reviewing the code, note that the word is converted to all upper case first, then each letter is translated ..
Preparing a table that compares actual and budgeted values : Honey Bear Confections is a small company dedicated to making bear-shaped sweets with honey as a sugar substitute. The problem related to Basic Accounting and it this problem preparing a table that compares actual and budgeted values of production a..
A university has the following dormitories : A university has the following dormitories: Allen Hall $1,500 persemester Pike Hall $1,600 persemester
Differential method of analysis : Explain the Integral and Differential method of analysis for finding the rate of reaction.(May-2005)2.For the elementary reaction in series Ak1Rk2S if k1= k2and at t = 0, CA= CAoandCRo= CSo= 0, find the maximum concentration of R.
Calculation or a calendar of some activity. : 1. Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields. The fields should be private.Create a test class to construct and call the methods of your class. Describe your class and..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a fraction class called fraction.java

Write a Fraction class called Fraction.java that implements these methods: add - This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply - This method receives a Fraction parameter and multip..

  What is the purpose of the restrict keyword in c99

Arrays of strings in C and C++ can also be initialized with string literals. In this case, the array is one of pointers to characters.

  Write the java code for classes team and player

Write the Java code for classes Team and Player. Declare the classes in package com.myleague.reg. Provide an implementation of all methods according to the given method names. You can use the class Random fromjava.util to randomly pick a student

  Display the value stored at the index

Display the value stored at the index. Take a screenshot to verify that the words in your Array or ArrayList are displayed successfully.

  Create a class rectangle with attributes length and width

Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member function that calculate and the area of the rectangle.

  What are the equilibrium price and quantity of corn

The table above contains information about the corn market. Use the table to answer the following questions. a.       What are the equilibrium price and quantity of corn? b.       Suppose the prevailing price is $9 per bushel. Is there a shortage o..

  Create an application that provides a solution

Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description.

  Calculate the maximum data rate in mb/second

b)How much cylinder skew is needed for an Oceangate disk operating at 20,000 rpm(new series for the Christmas promotions) which has a seek time of 0.5 milliseconds? This disk has 800 sectors of 512 bytes each on each track.

  Write a on the popularity of java

Write a 4-5-page research-oriented paper in APA format that focuses on the "Popularity of Java". The paper must cite at least 5 references, not including the textbook or the Bible.

  Create a road class

Create a Road class. The class should have functions for setting the width of the road in feet and the length of the road in miles. It should also have functions for obtaining the width of the road the length of the road.

  Creates an array of six divsales objects

Write a program that creates an array of six DivSales objects. The program should ask the user to enter the sales for four quarters for each division.

  Write java program which will permit user to input data

Write the Java Program which will permit the user to input data. The data will be validated using a loop that requires the user to input the data until it is correct or in the correct range. T

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