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): q

Getting 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!

hate it to tell you, but if I look at the assignment and then your code, I can't help but feeling you're way off.

best thing is to split the assignment in logical steps, and to try and write and combine those to a working piece of code.

To add what stultuske suggested, your flow is a bit intuitive. What you should do could be as follows.

1)In your constructor, you need to read data from a file, map & count each word, and store the treemap data which is a class variable.
2)Go into a loop asking users to enter a word.
3)Once an input is entered, check whether or not your treemap contains the word. If it does, return the count value (the value of the treemap using the word as key); otherwise, return 0 (not found).

Your current code attempts to combine #1 and #2 together. Even though it will map data once before a loop, the mapping should be inside your constructor because you need to map only once and will not need to map again regardless any method call.

This article has been dead for over six months. Start a new discussion instead.