In this assignment you are to implement a hash function and
use a Java sort through Java Utilities - they pretty much do it for you!
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
Hashtable<String, Integer> hash = new Hashtable<>();
- Request name of input file from user
- Open the file
- Read a line of text
- Convert the text to all uppercase (String method)
- Replace everything except the letters A-Z with a space:
- "I-Can't!" becomes "I CAN T"
String[] words = read.nextLine().toUpperCase().replaceAll("[^A-Z]", " ").split(" ");
- The spaces will now be assumed to delimit words
- Thus, T become a word rather than worry about single quotes versus apostrophes.
- Count each word by:
- If it is not already in your hash table (hash.containsKey(words[i])), add it
hash.put(words[i], 1)
- Otherwise, add one to the number of times this word has been seen
hash.put(words[index], (hash.get(words[index]))+1)
- Repeat to end of file
- Sort the hash table using a Java sort
- You can use a Java Treemap.
TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>(hash); //Use Tree Map on your hash table
- Using an iterator, print each word and the count of how many times it occurred, one per line in alphabetical order.
Iterator itr = treeMap.keySet().iterator();
while (itr.hasNext())
{
word = itr.next();
//print word and treeMap's value
}
Sample Data Sets:
Submission
Place your code in the D2L dropbox
Place a text document of your output for one of the datasets in the folder