I'm trying to run through a previously made TreeMap and convert it into a new one based on what the user inputs. I'm creating a hangman gave and the input is the user's guess and I want to make it so the list narrows based on what the user inputs. For example, if they guess "A", all words with the letter "A" in them will go to group 1. While words without the letter "A" will go in group 2. The group with the largest sum of entries will be returned.
The only thing that doesn't seem to work is when I try to get an entry from the dictionary and declare it as a String so I can compare chars in the string to the input.
The only thing that doesn't seem to work is when I try to get an entry from the dictionary and declare it as a String so I can compare chars in the string to the input.
JavaScript:
public static TreeMap generateList(TreeMap<String,String> dictionary, char input) {
TreeMap<String,String> group1 = new TreeMap<String,String>();
TreeMap<String,String> group2 = new TreeMap<String,String>();
for (int k=0; dictionary.firstEntry()!=dictionary.lastEntry(); k++) {
String tem = (String) dictionary.get(dictionary.lastKey()); ////// doesn't work
dictionary.remove(dictionary.lastKey());
//System.out.println(dictionary);
// System.out.println(tem); these two are for testing purposes
for (int i = 0; i<tem.length(); i++) {
if (tem.charAt(i) == input) {
//if (group1.containsKey(tem)) {
group1.put(tem, tem);
//}
} else if (i == tem.length()){ //&& !group1.containsKey(tem)){
group2.put(tem, tem);
}
}
}
if (group1.size() > group2.size()) {
return group1;
} else {
return group2;
}
}