Question

Read the words in from the binary file and figure out how many times each word...

Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user.

Use ObjectInputStream to read binary file

Use a HashMap with the word as a key (String) and an Integer as the value. For each word, first check to see if it already exists in the Map. If not, add the word as key with a value of 1 for the Integer value. If it does, get the current value and increment by 1 – replace the old key,value with the new key, value. After all the words are processed and the HashMap is complete. Iterate through the Hash map and display the results in a user -friendly fashion.

Create an easy to read table and print to the console. Hint: Use tabs (/t) (values are not from file)

Example:

Word                     Count

A                             190

Apple                        6

Etc

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks



//WordCount.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class WordCount {
        // this is just a helper method to create a sample binary file with some
        // strings. just in case if you don't have one.
        private static void createBinaryFile(String filename) throws IOException {
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                                new File(filename)));
                String words[] = { "these", "are", "some", "some", "words", "for",
                                "testing", "these", "words" };
                for (String str : words) {
                        out.writeObject(str);
                }
                out.close();
        }

        public static void main(String[] args) throws Exception {
                // replace below with your input file name
                String filename = "words.dat";
                // uncomment below line if you want to create a binary file first, with
                // some basic words (this will overwrite your file, if it already exist)

                // createBinaryFile(filename);

                // creating a hashmap
                HashMap<String, Integer> map = new HashMap<String, Integer>();
                // opening binary file in read mode
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                                new File(filename)));

                // looping
                while (true) {
                        // trying to read a String. if an exception occur, exiting loop
                        // (that is when there is no more data to read)
                        try {
                                String str = (String) in.readObject();
                                // if map does not contain str as key, adding to map with
                                // value=0
                                if (!map.containsKey(str)) {
                                        map.put(str, 0);
                                }
                                // fetching current value, adding 1 to it
                                map.put(str, map.get(str) + 1);
                        } catch (Exception e) {
                                break;
                        }

                }
                // closing file
                in.close();
                // now looping and displaying each word and its count. note that the
                // output may not be in any order. that's the thing about hashmaps, they
                // don't preserve any order.
                System.out.println("Word\t\tCount");
                for (String str : map.keySet()) {
                        System.out.println(str + "\t\t" + map.get(str));
                }
        }
}

/*OUTPUT (for some input file)*/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Identify a true statement about prelinguistic vocalizations. A. Babbling is not inborn. B. Deaf children cannot...
    asked 8 minutes ago
  • write a note summarizing the evidence for and against expansionary fiscal policy causing higher GDP growth...
    asked 9 minutes ago
  • please show all work In order to estimate the mean amount of time computer users spend...
    asked 18 minutes ago
  • There is natural conflicct in radilogy between providing high subject contrast for image formation and kepping...
    asked 53 minutes ago
  • Marni is 31 years old and 35 weeks pregnant. She was initially seen in the emergency...
    asked 1 hour ago
  • When designing a network of distribution centers to serve a large market, like the U.S., it...
    asked 1 hour ago
  • 1. According to psycholinguistic theory, _____ . A. language acquisition devices hinder the process of language...
    asked 2 hours ago
  • Following are selected accounts for Mergaronite Company and Hill, Inc., as of December 31, 2018. Several...
    asked 2 hours ago
  • Should welfare recipients be required to work? If so, what should happen to their children while...
    asked 2 hours ago
  • Calculate the concentrations of all species in a 0.580 M Na2SO3 (sodium sulfite) solution. The ionization...
    asked 2 hours ago
  • 1. A(n) _____ is an example of Simple Harmonic Motion. ticking wrist-watch oscillating mass on a...
    asked 2 hours ago
  • Year Stock Price 2014 $         22.25 2015 $         22.50 2016 $         22.00 2017 $   &nbsp
    asked 2 hours ago