Write a program in Java to count the occurrences of each word in a large text fi
ID: 3827339 • Letter: W
Question
Write a program in Java to count the occurrences of each word in a large text file (like a book)
Allow the user to type a word and report how many times that word appeared in the book.
Report all words that appeared in the book at least 500 times, in alphabetical order.
Write the solution as a Class
A HashMap to store the words and times it appears
Constructor accepting a file or a path String
reportWord(String word) method return the times the given word parameter appears
reportWordOver(int times) method that output all words that appears more than the given times.
Explanation / Answer
Below is the class i wrote for your problem.
import java.io.*;
import java.util.HashMap;
import java.util.Set;
public class WordCount
{
HashMap<String, Integer> wordCount=new HashMap<String, Integer>();
public WordCount(String file)
{
// The name of the file to open.
String fileName = file;
// This will reference one line at a time
String line = null;
Integer count;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
//read one by one line. then retrieves a one by one word.
while((line = bufferedReader.readLine()) != null)
{
String[] words =line.split(" ");
for(String word : words)
{
word.trim();// trims the spaces in front and rear in word if exists
if(wordCount.containsKey(word))
{
// if word is allready in hashmap then, increase its count by one.
count=wordCount.get(word)+1;
wordCount.put(word, count);
}
else
{
//it will come in this part if the word has occured for first time. so assign the count as 1.
wordCount.put(word, 1);
}
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex)
{
System.out.println("Error reading file '"+ fileName + "'because of "+ex );
}
}
public int reportWord(String word)
{
//will check if word present in hashmap if not then will return the -1. indicati ng the invalid word has been passed to this method
if(wordCount.get(word)!=null)
return wordCount.get(word);
return -1;
}
public void reportWordOver(int times)
{
Set<String> words=wordCount.keySet();
System.out.println("Below are the Words which appeared more than "+times+" times");
for(String word : words)
{
if(wordCount.get(word)>times)
System.out.println(word);
}
}
}
Using this class as below:
public static void main(String args[]) throws IOException
{
WordCount wordCount=new WordCount("temp.txt");
System.out.println(wordCount.reportWord("abbcc"));
wordCount.reportWordOver(1);
}
this is running code. should solve your problem.
please let me know your feedback for this answer and comment if require something more.
thanks & regards
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.