Need help with computer science lab Your task today is to write a program that r
ID: 3709940 • Letter: N
Question
Need help with computer science lab
Your task today is to write a program that runs through the mobyDick.txt file that is provided. Your program should print out any word that appears more than 2000 times in the book.
In an earlier example, we counted the number of unique words in the book Moby Dick. What if we wanted to find the words that occur most frequently in the book? To do this, we should count how many times each word in the book occures, then examine all of those counts and print the ones with the largest values.
Maps are very useful for solving these kinds of problems. We can create a word-count map in which each key is a word and its associated value is the number of occurrences of that word in the book:
wordCountMap = empty.
for(each word from file) {
if(I have never seen this word before) {
set this word's count to 1.
} else {
increase this word's count by one.
}
}
We'll need a Scanner to read the appropriate file and a Map to store the word counts. We'll use a TreeMap so that the frequently used words will be shown in alphabetical order.
We can now read the file's contents and store each word that the program encounters in the map. If we come across a word we have already seen, we retrieve its old count value, increment it by 1, and put the new value back into the map. Recall that when you put a key/value mapping into a map that already contains that key, the old mapping is replaced.
Once you have the map created, you now have everything you need to find any word that appears more than 2000 times. Your project should have a main function that reads in the file and uses a second method to create the map from each word to its wordcount. Once it has the map, the main function should print out all words that appear more than 2000 times.
Your second function should create the map itself. A method outline would look something like this:
public static Map<String, Integer> getCountMap(Scanner in) {
}
Explanation / Answer
Code to copy-
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
//Create file object, please change file path according your system
File file = new File("C:\Users\HP\Desktop\mobyDick.txt");
//Create scanner object and pass file into its constructor
Scanner in = new Scanner(file);
//Invoke getCountMap(), this will return a Map that has every word's count
Map<String, Integer> wordCountMap = getCountMap(in);
// get EntrySet from wordCountMap, returns Set of entries which are present in wordCountMap
Set<Entry<String, Integer>> entrySet = wordCountMap.entrySet();
//Iterate over the entrySet using for each loop, this will give each entry present in EntrySet
for (Entry<String, Integer> entry : entrySet) {
String word = entry.getKey();//This is the word we have in map
Integer count = entry.getValue();// this is count we have corresponding a word
//check if count is greater then 2000, and if yes then print that word
if(count>2000){
System.out.println(word);
}
}
}
public static Map<String, Integer> getCountMap(Scanner in) {
Map<String, Integer> wordCountMap = new LinkedHashMap<>();
while (in.hasNext()) {
String word = in.next();
if(!wordCountMap.containsKey(word)){// means first occurence
wordCountMap.put(word, 1);//add this word into map with count one
}else{
int currectCount = wordCountMap.get(word);// get current count
currectCount++;//increment current count by one
wordCountMap.put(word, currectCount);//override the word count with incremented value
}
}
return wordCountMap;
}
}