There was a need to create a dictionary class, the method of which for each word returns its text explanation. Initialize the dictionary with several words.
In this case, always returns null . What have I done wrong?
pulic class Dict extends HashMap { HashMap<String, String> dict = new HashMap<String, String>(); String first; String second; public Dict(String first, String second) { dict.put(first, second); } public Dict() {} public void put(String first, String second) { dict.put(first, second); } public void printDict(String first) { System.out.println(dict.get(this.first)); } } Main:
public class Main { public static void main(String[] args) { Dict dict = new Dict(); dict.put("я", "мы"); dict.put("ты", "вы"); dict.put("он, она, оно", "они"); dict.printDict("я"); } }
HashMap<String, String> dict = new HashMap<String, String>();By the way, you can shorten it toHashMap<String, String> dict = new HashMap<>();. - Regentextends HashMapandHashMap<String, String> dictvery strange. If you do not needDictto be able to do everything the same asHashMap, thenextends HashMapshould be removed. - Regent