I've got an issue with some current code. I finished the first half, but now I'm using a hashMap to search this imported text document and find the most used word and the number of times it appears. I had a lot of syntax written and my professor said it was a little extraneous so I whittled it down some. The problem I'm having is with entrySet, which we have to use. I've gotten it to this point, but I'm having an issue implementing it. Any help would be great, I'm just unfamiliar with the entrySet concept but I've read through the documentation.
Thanks, here's the code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class HashMapPractice {
public static void main(String[] args) throws FileNotFoundException{
Scanner file = new Scanner(new File("crime_and_punishment.txt"));
Map<String, Integer> crimeMap= new HashMap<String, Integer>();
while (file.hasNext()){
String word = file.next();
if(crimeMap.containsKey(word)){
int placeholder = crimeMap.get(word) + 1;
crimeMap.put(word, placeholder);
}else{
crimeMap.put(word, 1);
}
}//ends while loop
Set<Map.Entry<String,Integer>> entrySet = crimeMap.entrySet();
for(Map.Entry<String, Integer> ent : entrySet){
String key = ent.getKey();
Integer value = ent.getValue();
System.out.printf("Word: [" + ent.getKey()+"], Times: [" +
ent.getValue()+"]");
}
}//ends main
}//ends class