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
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)*/
Get Answers For Free
Most questions answered within 1 hours.