I am attempting to create a gui application that reads english user input and translates to morse (http://ascii-table.com/morse-code.php). I have touched on the basic parts of the program so far. My question is; what is the best way of going about reading morse? should i create a text file to import the morse letters off or should i declare each of them inside the program to translate? Next question is how would i go about doing this? please refer to a tutorial if possible. Thanks for your time.
Since Morse Code is not likely to change, hard-coding the mapping of characters to code strings is a valid option:
private static Map<Character,String> charToCode = new HashMap<Character,String>();
{
charToCode.put('A', ".-");
charToCode.put('B', "-...");
...
charToCode.put('Z', "--..");
}
This map lets you convert messages to code one character at a time:
StringBuilder
for the resultcharAt(i)
for thatcharToCode.get(upperChar)
to look up the code representation of the characterStringBuilder
; append a space after itStringBuilder
to String
, and put it on the label.