20.14 (Counting Letters) Modify the program of Fig. 20.18 to count the number of
ID: 668481 • Letter: 2
Question
20.14 (Counting Letters) Modify the program of Fig. 20.18 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, one T and one R. Display the results.
//Fig. 20.18: 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< String, Integer > myMap = new HashMap< String, Integer >();
createMap( myMap ); // create map based on user input
displayMap( myMap ); // display map content
} // end main
// create map from user input
private static void createMap( Map< String, Integer > map )
{
Scanner scanner = new Scanner( System.in ); // create scanner
System.out.println( "Enter a string:" ); // prompt for user input
String input = scanner.nextLine();
// 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
} // end if
else
map.put( word, 1 ); // add new word with a count of 1 to map
} // end for
} // end method createMap
// display map content
private static void displayMap( Map< String, Integer > map )
{
Set< String > keys = map.keySet(); // get keys
// sort keys
TreeSet< String > sortedKeys = new TreeSet< String >( keys );
System.out.println( " Map contains: Key Value" );
// generate output for each key in map
for ( String key : sortedKeys )
System.out.printf( "%-10s%10s ", key, map.get( key ) );
System.out.printf(
" size: %d isEmpty: %b ", map.size(), map.isEmpty() );
} // end method displayMap
} // end class WordTypeCount
Explanation / Answer
//Fig. 20.18: 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< String, Integer > myMap = new HashMap< String, Integer >();
private HashMap map = new HashMap();
createMap( myMap ); // create map based on user input
displayMap( myMap ); // display map content
} // end main
private void createLettersMap()
{
String input;
//create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//inputting first string
System.out.println("Enter first string: ");
input=keyboard.nextLine();
input.toLowerCase();
for(int i = 0; i < input.length(); i++)
{
char letter = input.charAt(i);
if (Character.isLetter(letter))
{
String key = String.valueOf(letter);
//if the map contains the work
if(map.containsKey(key))
{
//get the value
Integer count = (Integer) map.get(key);
//and increment it
map.put(key, new Integer(count.intValue() + 1));
}
else //otherwise add the word with a value of 1
map.put(key,new Integer(1));
}
}//end for
}// end method createLettersMap
private String createLettersOutput() {
Set keys = map.keySet();
Iterator keyIterator = keys.iterator();
String output = " ";
// iterate through the keys
while (keyIterator.hasNext()) {
Object currentKey = keyIterator.next();
// output the key value pairs
output += currentKey + " " + map.get(currentKey) + " ";
}
return output;
}// end method createLettersOutput
// create map from user input
private static void createMap( Map< String, Integer > map )
{
Scanner scanner = new Scanner( System.in ); // create scanner
System.out.println( "Enter a string:" ); // prompt for user input
String input = scanner.nextLine();
// 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
} // end if
else
map.put( word, 1 ); // add new word with a count of 1 to map
} // end for
} // end method createMap
// display map content
private static void displayMap( Map< String, Integer > map )
{
Set< String > keys = map.keySet(); // get keys
// sort keys
TreeSet< String > sortedKeys = new TreeSet< String >( keys );
System.out.println( " Map contains: Key Value" );
// generate output for each key in map
for ( String key : sortedKeys )
System.out.printf( "%-10s%10s ", key, map.get( key ) );
System.out.printf(
" size: %d isEmpty: %b ", map.size(), map.isEmpty() );
} // end method displayMap
} // end class WordTypeCount
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.