Question

In: Computer Science

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

Solutions

Expert Solution

SOLUTION:

Here is the code for the following problem.

I have also inserted comment at proper places so that you can easily understand the flow of code.

Please put your binary file in the same folder where you are storing your java file.

Also, if you have another name for the file, change the name in code also

----------------------------------------------------------------------------------------------------------------------------------

Binary File: input.dat:

This is the paragraph that I wrote to test the frequency of each word in the file.
However you can make your own and test with this code.

Code:

import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) {
  
  
// Creating a HashMap containing String as a key and occurrences as a value
HashMap<String, Integer> charCountMap = new HashMap<String, Integer>();
  
//read the binary file
try
{
File file = new File("input.dat");
Scanner scnr = new Scanner(file);
  
//read each word line by line and count the frequency
while(scnr.hasNext()){
  
//read next word
String word = scnr.next();

//count the frequency
if (charCountMap.containsKey(word)) {
  
// If word is present in charCountMap,
// incrementing it's count by 1
charCountMap.put(word, charCountMap.get(word)
+ 1);
}
else {
  
// If word is not present in charCountMap,
// putting this char to charCountMap with 1 as it's value
charCountMap.put(word, 1);
}
}
}
catch(Exception e)
{
//show an error message if unable to read the file
}
  
//display the frequency of each word
System.out.println("Word\t\tCount");
for (Map.Entry entry : charCountMap.entrySet()) {
System.out.printf("%-18s %d\n", entry.getKey(),entry.getValue());
}
  
}
}

Output

Thank you,


Related Solutions

Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
a)     How many four-letter words can be formed from the letters of the word LAUNDRY if each...
a)     How many four-letter words can be formed from the letters of the word LAUNDRY if each letter can only be used one time in a word? Y is NOT considered a vowel in this word. b)    How many contain the letter Y? c)     How many contain both vowels? d)    How many of them contain exactly three consonants? e)    How many of them begin and end in a consonant? f)      How many begin with N and end in a vowel? g)     How many begin with N and...
Done in c++, Read an unsorted keywords file once to determine how many words are in...
Done in c++, Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
QUESTION : Read from a file that contains a paragraph of words. Put all the words...
QUESTION : Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT