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

  Threads and synchronization

Threads and Synchronization- Whenever we have multiple entities working together to get something done, we have the problem of synchronization between the entities

  Define the determinant of a matrix

Define the determinant of a matrix a (written det(a)) recursively as follows: if a is a 1 x 1 matrix, then det(a) = x

  Concept of java socket programming

Designed to asses learning outcomes and write programs that would communicate with another program running in the network

  Program designs used in developing java applications

program designs used in developing Java applications

  Write a recursive program

Write a recursive program to compute the number of ways in which an integer k can be written as sum

  Complete the classes cipher - caesar and transpose

Complete the classes Cipher, Caesar, and Transpose, and Reverser - Define an interface called Constants that will store the value 26 in the identifier WRAP_AROUND;

  User interface menu using doubly linked lists

Each mNode defines the main menu item name, two links to its mNode neighbors (the previous and next mNode) and a link to its first submenu node.

  Build the gui layout of the gamecreate a class called

build the gui layout of the gamecreate a class called pipegameapp.java which will be the main game user interface. the

  Write a program that assigns seats on an airplane

Prepare a program that teaches arithmetic to a young child. The program tests subtraction and addition. Write a program that assigns seats on an airplane.

  A java program that reads the records

A java program that reads the records from the golf.dat file and displays them. It also displays the name of the player with the best (lowest) golf score.

  How should ijvm be modified to make best use of instructions

On the full JVM machine, there are special 1-byte opcodes for loading locals 0 through 3 onto the stack instead of using the general ILOAD instruction. How should IJVM be modified to make the best use of these instructions?

  Case study - greenhouse humidity control

The control system that will respond to sensor information to turn on/off a misting device, turn on/off a watering system and open/close ventilation windows.

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