I am trying to make the program read four .txt files and count the words in those files.
private HashMap<String, ArrayList<String>> mapWords = new HashMap<String, ArrayList<String>>();
public void countWordsInFiles() {
buildWordFileMap();
}
private void buildWordFileMap() {
mapWords.clear();
String[] files = {"brief1.txt","brief2.txt","brief3.txt","brief4.txt"};
for(int i=0; i<files.length; i++){
FileResource resource = new FileResource("data/" + files[i]);
addWordsFromFile(resource);
}
}
private void addWordsFromFile(FileResource resource) {
for(String word : resource.words()){
word = word.toLowerCase();
if (mapWords.keySet().contains(word)){
mapWords.put(word, //Not sure how to use arraylist here);
}
else {
mapWords.put(word, //Not sure how to use arraylist here);
}
}
}
The problem is I'm not sure how to make the "if" in the method "addWordsFromFile". Basically, I want my output to be something like this:
The greatest number of files a word appears in is three, and there are two such words: “cats” and “and”.
“cats” appears in the files: brief1.txt, brief3.txt, brief4.txt
“and” appears in the files: brief1.txt, brief3.txt, brief4.txt
mapWords.containsKeyinstead ofmapWords.keySet().contains