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
Solve Project 2 using a TreeMap. You can display the words in key sequence without performing...
Solve Project 2 using a TreeMap. You can display the words in key sequence without performing a sort. Java Project 2: Use a HashMap to store the frequency counts for all the words in a large text document. When you are done, display the contents of this HashMap. Next, create a set view of the Map and store its contents in an array. Then sort the array based on key value and display it. Finally, sort the array in decreasing...
Instructions: Read in the attached text file provided to you into your program. Next, use mapping...
Instructions: Read in the attached text file provided to you into your program. Next, use mapping (hash map, or tree map) to keep track of a word and the number of occurrences of each word in the text file. Count the words in a case-insensitive fashion ("good" and "Good" to be the same word). Make sure that the punctuation marks are not part of a word. Display the output in alphabetical order of words and its occurrence count. Attached text...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...