*
The moose likes Beginning Java and the fly likes converting morse code to english Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting morse code to english" Watch "converting morse code to english" New topic
Author

converting morse code to english

Khair Bahir
Greenhorn

Joined: Aug 01, 2011
Posts: 14
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.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all you probably can implement the conversion from letter to morse-code more efficient with an array or Map instead of those if-statements.

For stringToEnglish: split the morse-code on "|" and do the opposite of creating morse-code based on the letter.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
. . . and check whether your Morse code is consistent. You have ".- " for a, with a space in, and no spaces for any other letters. That will cause errors.
Ashish Schottky
Ranch Hand

Joined: Dec 29, 2009
Posts: 93
@Khair: You can do something like this:

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
As suggested earlier, a Map is a far better solution.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: converting morse code to english