java - Search through HashMap with user input string -
i creating dictionary application small assignment, these were, unfortunately, given out random , have pulled 1 have no idea how implement.
i hear suggestions taking user input , using through hashmap (is possible?) provide definition.
additionally (sorry), has bilingual, have words in both languages in 1 hashmap , have way search through both?
i out of league here, appreciated.
import java.util.hashmap; import java.util.map; import java.io.*; public class dictionary { public static void main(string[] args) { map<string, string> map = new hashmap<string, string>(); string s1 = getinput("enter word define: "); map.put ("abduction","a carrying away of person against or illegally."); map.put ("ache","to in pain or distress."); map.put ("befriend","to friend to, when in need."); map.put ("bilingual","speaking 2 languages."); map.put ("comical","something funny."); map.put ("comprehensive","large in scope or content."); map.put ("deplete","to reduce or lessen, use, exhaustion or waste."); map.put ("detest","to dislike or hate intensity."); } private static string getinput(string prompt) { bufferedreader stdin = new bufferedreader( new inputstreamreader(system.in)); system.out.print(prompt); system.out.flush(); try { return stdin.readline(); } catch (exception e) { return "error: " + e.getmessage(); } } }
you've entirely missed point of hashmap
if you're attempting search through it. hashmap
designed subvert need search, using value key
.
if(map.containskey(userinput)) { return map.get(userinput); }
this meant hashmap
having o(1)
complexity when searching. ever need iterate or recurse once.
additionally (sorry), has bilingual, have words in both languages in 1 hashmap , have way search through both?
yes. add new entries hashmap
.
map.put("hello", "a greeting"); map.put("hola", "una greetingo"); // forgive racist spanish.
extra (in case necessary) reading
- read on hashmap. need learn basic stuff object. you're out of depth because haven't spent appropriate time learning problem.
Comments
Post a Comment