Populating a Hashmap in Java -
ok having quite bit of issue. want populate hash map set of string keywords. basically, user enters keyword , if matches keyword in hash map parsefile method called. else if user input synonym of keyword, search hash map original keyword. issue populating hash map. can guide me in right direction?
public static void main(string[] args) throws filenotfoundexception { /* synonym map of keywords , have populate before user can give values.*/ hashmap<string, string[]> synonymmap = new hashmap<string, string[]>(); populatesynonymmap(); //populate map /*get user response*/ system.out.println("what know?"); system.out.print("> "); scanner scanner = new scanner(system.in); string input = scanner.nextline().tolowercase(); /*first check if input original one*/ if (synonymmap.containskey(input)) { parsefile(input); } else { /*if not search synonymmap original keyword*/ (map.entry<string,string[]> entry : synonymmap.entryset()) { string[] value = entry.getvalue(); if (arrays.aslist(value).contains(input)) { parsefile(entry.getkey()); break; } } } }
may can try following code. accomplish. think need think of redesigning way looking keyword synonym.
and if facing issues populating hashmap may because defining map inside main method , not populating anywhere else.
so second option, can change assignment in main method ..
synonymmap = populatesynonymmap(); // populate map
and sample method populate map.
private static hashmap<string, string[]> populatesynonymmap() { hashmap<string, string[]> synonymmap = new hashmap<string, string[]>(); string s[] = new string[] { "x1", "x2", "x3" }; string t[] = new string[] { "y1", "y2", "y3" }; string u[] = new string[] { "z1", "z2", "z3" }; synonymmap.put("x", s); synonymmap.put("y", t); synonymmap.put("z", u); return synonymmap; }
Comments
Post a Comment