Question: In this program you will ask the user for a String, and then output the count of every word in that String in alphabetical order.

In this program you will ask the user for a String, and then output the count of every word in that String in alphabetical order. Youll need to use a HashMap to do this.

For example if the user entered:

Hello hello world 

Youd print out hello: 2 world: 1

Since the word hello appeared twice and the word world appeared once.

HINT:

Use the provided printSortedHashMap method to print out the HashMap in alphabetical order.

import java.util.*;

public class WordCounts extends ConsoleProgram { public void run() { // Start here! } /* * This method takes a HashMap of word counts and prints out * each word and it's associated count in alphabetical order. * * @param wordCount The HashMap mapping words to each word's frequency count */ private void printSortedHashMap(HashMap wordCount){ // Sort all the keys (words) in the HashMap Object[] keys = wordCount.keySet().toArray(); Arrays.sort(keys); // Print out each word and it's associated count for (Object word : keys) { int val = wordCount.get(word); System.out.println(word + ": " + val); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!