So this is basically what's supposed to be the outcome of my code:
69:hw/05/src> javac WordOccurrenceCounter.java
70:hw/05/src> java WordOccurrenceCounter
Reading the book...
Getting counts from a TreeMap...
Enter a string ('q' to quit): wealth
wealth occurred 7 times.
Enter a string ('q' to quit): poverty
poverty occurred 10 times.
Enter a string ('q' to quit): pestilence
pestilence occurred 1 times.
Enter a string ('q' to quit): man
man occurred 1193 times.
Enter a string ('q' to quit): woman
woman occurred 230 times.
Enter a string ('q' to quit): unix
unix occurred 0 times.
Enter a string ('q' to quit): qGetting counts from a HashMap...
Enter a string ('q' to quit): wealth
wealth occurred 7 times.
Enter a string ('q' to quit): poverty
poverty occurred 10 times.
Enter a string ('q' to quit): pestilence
pestilence occurred 1 times.
Enter a string ('q' to quit): man
man occurred 1193 times.
Enter a string ('q' to quit): woman
woman occurred 230 times.
Enter a string ('q' to quit): unix
unix occurred 0 times.
Enter a string ('q' to quit): q
This is the code I have so far:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
// The example input data is Les Miserables by Victor Hugo translated by
// Isabel F. Hapgood, and was obtained from Project Gutenburg at
// http://www.gutenberg.org/cache/epub/135/pg135.txt
public class WordOccurrenceCounter
{
private LinkedList <String> words;
private Map<String, Integer> theMap;
private Scanner in;
private Scanner userInput;
public WordOccurrenceCounter(String fileName)
throws FileNotFoundException
{
words = new LinkedList<String>();
in = new Scanner(new FileInputStream(fileName));
userInput = new Scanner(System.in);
} // constructor
// Read the words from a file into a linked list.
private void read()
{
while (in.hasNext())
words.add(in.next());
in.close();
} // read()
// Fill the desired map, and allow the user to make queries into the
// map.
private void interact() {
fillMap();
String word = queryUser();
while (!word.equals("q")) {
if (theMap.containsKey(word))
System.out.println(word
+ " occurred "
+ theMap.get(word)
+ " times.");
else
System.out.println(word
+ " occurred 0 times.");
// Get the next word.
word = queryUser();
} // while()
} // interact()
// Ask the user for a word to search for. Remind the user that a 'q'
// will be interpreted as a request to quit.
public String queryUser() {
System.out.print("Enter a string ('q' to quit): ");
return userInput.next();
} // queryUser()
private void fillMap()
{
//This is where I'm stuck
} // fillMap()
// Place all the words in the book into a linked list. Then copy that
// linked list into a TreeMap. Allow the user to interact with the
// words counts in the TreeMap. Then repeat the process with a
// HashMap.
public void run() {
System.out.println("Reading the book...");
read();
theMap = new TreeMap<String, Integer>();
System.out.println("Getting counts from a TreeMap...");
interact();
theMap = new HashMap<String, Integer>();
System.out.println("\nGetting counts from a HashMap...");
interact();
} // run()
public static void main(String[] args)
throws FileNotFoundException
{
WordOccurrenceCounter woc = new WordOccurrenceCounter("pg135.txt");
woc.run();
} // main()
} // class WordOccurrenceCounter
Thank you!