Question: 1. Modify the program of Fig. 16.17 (attached) to count the number of occurrences of each letter rather than of each word. For example, the
1. Modify the program of Fig. 16.17 (attached) to count the number of occurrences of each letter rather than of each word. For example, the string "HELLO THERE" contains two Hs, three Es, two Ls, one O and one T and one R. Display the results.
provided code ***********************************************************
// Fig. 16.17: WordTypeCount.java // Program counts the number of occurrences of each word in a String. import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.TreeSet; import java.util.Scanner;
public class WordTypeCount { public static void main(String[] args) { // create HashMap to store String keys and Integer values Map
createMap(myMap); // create map based on user input displayMap(myMap); // display map content }
// create map from user input private static void createMap(Map
// tokenize the input String[] tokens = input.split(" "); // processing input text for (String token : tokens) { String word = token.toLowerCase(); // get lowercase word // if the map contains the word if (map.containsKey(word)) { // is word in map? int count = map.get(word); // get current count map.put(word, count + 1); // increment count } else { map.put(word, 1); // add new word with a count of 1 to map } } } // display map content private static void displayMap(Map
// sort keys TreeSet
System.out.printf("%nMap contains:%nKey\t\tValue%n");
// generate output for each key in map for (String key : sortedKeys) { System.out.printf("%-10s%10s%n", key, map.get(key)); } System.out.printf( "%nsize: %d%nisEmpty: %b%n", map.size(), map.isEmpty()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
