Java Write a program that will read in multiple lines of tex

Java:

Write a program that will read in multiple lines of text and output a list of all the letters that occur in the text, along with the number of times each letter occurs. The last line should end with a period, which will serve as a sentinel value.

Once the last line has been entered, the counts for all letters entered by the user should be listed in alphabetical order as they are output. Use an array of base type int of length 26, so that each indexed variable contains the count of how many letters there are. Array indexed variable 0 contains the number of a’s, array indexed variable 1 contains the number of b’s and so forth. Allow both uppercase and lowercase letters as input, but treat uppercase and lowercase versions of the same letter as being equal.

Hints: You will want to use one of the methods toUpperCase or toLowerCase in the wrapper classCharacter as described in Chapter 5. You will find it helpful to define a method that takes a character as an argument and returns an int value that is the correct index for that character, such as ‘a’ returning 0, ‘b’ returning 1, and so forth. Note that you can use a type cast to change a char to an int, like (int)letter.   This will not get the number you want, but if you subtract (int)\'a\', you will then have the right index. Allow the user to repeat this task until the user says she or he is through.

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CountLetters {
   /**
   * @param args
   */
   public static void main(String[] args) {

       try {
           BufferedReader in = new BufferedReader(new InputStreamReader(
                   System.in));
           String s;
           int array[] = new int[26];
           System.out.println(\"Enter the String (Ends with .):\");
           while ((s = in.readLine()) != null) {

               for (int i = 0; i < s.length(); i++) {
                   char ch = s.charAt(i);
                   int index = getIndex(ch);
                   if (index != -1) {
                       array[index]++;

                   }
               }

               if (s.contains(\".\"))
                   break;
           }
           System.out.println(\"Count Alphabets:\");
           for (int i = 0; i < array.length; i++) {
               System.out.println((char) (65 + i) + \" : \" + array[i]);
           }
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

   /**
   * method to get the index of alphabet
   *
   * @param ch
   * @return
   */
   public static int getIndex(char ch) {
       if (Character.isAlphabetic(ch)) {
           if (Character.isUpperCase(ch)) {

               return (int) ch - 65;
           } else
               return (int) ch - 97;

       } else {

           return -1;
       }

   }
}

OUTPUT:

Enter the String (Ends with .):
The worsst drought in the United States in neearly a century is expected to drive up the price of milk, beef and pork next yeer, the government said Wednesdaay,
as consumers bear some of the bruntt of the sweltering heat that is drivng up the cost of feed corrn.
Count Alphabets:
A : 11
B : 3
C : 6
D : 10
E : 34
F : 6
G : 4
H : 10
I : 11
J : 0
K : 2
L : 3
M : 4
N : 15
O : 13
P : 5
Q : 0
R : 16
S : 14
T : 24
U : 7
V : 3
W : 3
X : 2
Y : 4
Z : 0

Java: Write a program that will read in multiple lines of text and output a list of all the letters that occur in the text, along with the number of times each
Java: Write a program that will read in multiple lines of text and output a list of all the letters that occur in the text, along with the number of times each

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site