Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

Question in Chemical Engineering Please give Correct Answer 1 9 .

Answered: 1 week ago

Question

Explain the key areas in which service employees need training.

Answered: 1 week ago

Question

Understand the role of internal marketing and communications.

Answered: 1 week ago