I'm trying to set up a morse code-english converter by prompting the user for morse-->english translation or english--> morse translation, prompting the user to enter the text to be translated, and then finally translating it. So far, I've been able to do english-->morse code translations because that just involves encoding each individual letter. I'm having trouble with morse-->english translations though because that doesn't just involve decoding each individual character. The problem I'm having is I don't want the program to convert each individual dash and period to a letter, but rather a string of dashes and periods to a letter (..-. = f). I also want "|" to separate each word in morse code. For example, if morse code input is "-- -.-- | -. .- -- ." then translated output should be "my name". I implemented this successfully in the stringToMorse() method, but I don't know how I would do it in the stringToEnglish() method. Any ideas are appreciated.
Create a database /look up table sort of thing for morse<->english
say something like this.
Take the input as a string.
use a counter to count '|' from the current position, make a substring
search for this substring in morse code array,once found, printout the character from corresponding english array or put that in string.
in this way, you can eliminate the if statements for both types of conversions.
Joined: Oct 13, 2005
As suggested earlier, a Map is a far better solution.