0
votes

public class EvilHangman {

public Map<String, ArrayList<String>> hashmap = new HashMap<String, ArrayList<String>>(); 
public Map<String, ArrayList<String>> hashmappattern = new HashMap<String, ArrayList<String>>(); 
public ArrayList<String> wordlist = new ArrayList<String>(); 
private String basekey="";
   /*
public EvilHangman(){
     hashmap = new HashMap<String, ArrayList<String>>();
     wordlist= new ArrayList<String>();
}
*/
public void readDictionary(String filename, int wordLength) throws IOException{     
    FileReader fr = new FileReader(filename);
    BufferedReader br = new BufferedReader(fr);
    PrintWriter writer = new PrintWriter("temp.txt", "utf-8");
    String line = br.readLine();
    while (line!=null){
        if (wordLength == line.length()){
            writer.println(line);
            wordlist.add(line);
        }
        line = br.readLine();
    }

    for (int i=0;i<wordLength;i++){
        basekey+="_";
    }
    System.out.println(basekey+"length"+basekey.length());
    hashmap.put(basekey, wordlist);     
    writer.close();
    br.close();
}   


public boolean checkLetter(String aletter){

    for (int i=0;i<wordlist.size();i++){
        char[] key = new char[wordlist.size()];

        for (int j=0;j<wordlist.get(i).length();j++){
            if (wordlist.get(i).charAt(j)==aletter.charAt(0)){
                key[j] = wordlist.get(i).charAt(j);
            }else{
                key[j] = '_';
            }
        }

        String wordkey = String.valueOf(key);
        System.out.println("Key = "+wordkey);

        if (hashmappattern.containsKey(wordkey)){   
            hashmappattern.get(wordkey).add(wordlist.get(i));
        }else{
            hashmappattern.put(wordkey, wordlist.get(i));
        }       
    }
}

}

I'm trying to read arraylist wordlist and for each and every word i'm scanning if entered letter (aletter) is in the word. if it is there i'm putting that letter in to char array. after going through all word I 'll have patterns like a_ , a_b , etc. Then I have take these word as a key and values into hashmap. if same key is found I want to append data instead of replacing.(I have created arraylist but its not working because i cannot do put(string , string)). how should I solve this problem. I'm trying to make evilhangman using hashmap

1

1 Answers

0
votes

You need to create the ArrayList and fill it with the first element. Then add the list to the hashmap:

    if (hashmappattern.containsKey(wordkey)){   
        hashmappattern.get(wordkey).add(wordlist.get(i));
    }else{
        ArrayList<String> list = new ArrayList<>();
        list.add(wordList.get(i));
        hashmappattern.put(wordkey, list);
    }