user interface - Converting morse in java -
i creating program converts english morse. have english-morse(using hashmap) , 2 text boxes user input , show result. have no idea how actual converting without file import. right have set 2nd action perform label equal map (i figured wrong worth attempt). understand need use string builder dont know how use it. question is, how attempt fix issue?
these text fields:
text1.addactionlistener(new actionlistener() //text box user { public void actionperformed(actionevent e) { string input = text2.gettext(); label.settext(input); } }); button.addactionlistener(new actionlistener() //convert morse (text box) { public void actionperformed(actionevent e) { string input = text1.gettext(); label.settext(chartocode); } });
here english-morse
//morse code map<character,string> chartocode = new hashmap<character,string>(); chartocode.put('a', ".-"); chartocode.put('b', "-..."); chartocode.put('c', "-.-."); chartocode.put('d', "-.."); chartocode.put('e', "."); chartocode.put('f', "..-."); chartocode.put('g', "--."); chartocode.put('h', "...."); chartocode.put('i', "...."); chartocode.put('j', ".---"); chartocode.put('k', "-.-"); chartocode.put('l', ".-.."); chartocode.put('m', "--"); chartocode.put('n', "-."); chartocode.put('o', "---"); chartocode.put('p', ".--."); chartocode.put('q', "--.-"); chartocode.put('r', ".-."); chartocode.put('s', "..."); chartocode.put('t', "-"); chartocode.put('u', "..-"); chartocode.put('v', "...-"); chartocode.put('w', "..-"); chartocode.put('x', "-..-"); chartocode.put('y', "-.--"); chartocode.put('z', "--..");
thanks time lads.
i'd suggest looking @ each character in english string, looking morse translation in map, , appending translation in string builder instanciation. here's simple example:
stringbuilder builder = new stringbuilder(); for(char c : englishstring.getchars()) { builder.append(translationmap.get(c.tostring().touppercase())); } system.out.println(builder.tostring());
i hope helps!
Comments
Post a Comment