Reputation: 77
I recently had an assignment to convert English to Morse code and Morse Code to English. When inputting Morse Code, my teacher wants individual letters separated with 1 space, and words separated by a '|'. For example, - --- | -... . is "to be." I was able to get the English to Morse working perfectly, but I am at a loss for Morse to English. I don't know how to get the for loop to stop at the right point and match it up with one of the codes in the array.
Just a side note, my teacher doesn't like Scanner, so he uses his own system of inputs. I'm familiar with his methods, so avoiding Scanner would be appreciated
public class Project1
{
public static void main( String [] args )
{
String morse[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
"--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
"|",".---","..---","...--","....-",".....","-....","--...","---..","----.","-----"};
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890";
String inputType = new String();
inputType = Input.getString( "Is your phrase in Morse Code or English? Enter 'morse' for Morse Code and 'english' for English" );
if( inputType.equalsIgnoreCase("morse") )
morseToEnglish(alphabet, morse);
else if( inputType.equalsIgnoreCase("english") )
englishToMorse(alphabet, morse);
else
System.out.println("Your entry is invalid");
}
public static void englishToMorse(String alphabet, String morse[])
{
String phrase = Input.getString("Enter your english phrase.");
phrase = phrase.toUpperCase();
for( int i = 0; i < phrase.length(); i++ )
{
if( phrase.charAt(i) == ' ' )
{
System.out.print("| ");
continue;
}
for( int j = 0; j < alphabet.length(); j++ )
{
if( alphabet.charAt(j) == phrase.charAt(i) )
{
System.out.print( morse[j] + " " );
break;
}
}
}
}
public static void morseToEnglish(String alphabet, String morse[])
{
String morseCode = Input.getString("Enter a phrase in morse code.");
for( int i = 0; i < morseCode.length(); i++ )
{
for( int j = 0; j < morse.length; j++ )
{
if ( morse[j] == morseCode.charAt(i))
System.out.print( alphabet.charAt(j) );
}
}
}
}
Any pointers would be greatly appreciated. Oh, sorry if the morseToEnglish code looks incoherent and bad, I've tried many different things and am at a loss.
Upvotes: 4
Views: 4660
Reputation: 694
The morseToEnglish should just be a reverse of the englishToMorse algorithm.
Like you have before, you can loop through the input strong. The difference is that multiple characters in Morse make a single English character. So keep track of those in a local variable. When you find a space in the Morse, find the English character that matches. You could loop across your Norse array looking for a match, or make a separate mapping where the lookup key is the Morse symbol (something like a HashTable). And when you get to a pipe character, after finding the character from your local variable, instead of just continuing, you should add an English space to denote the edge of the word before continuing on.
Upvotes: 2
Reputation: 111
I would suggest using String.split(). You can split on |, then further split them on spaces. An enum type with a toString() that prints the character might help compartmentalize the conversion logic (if you don't know switches, this is a good chance to learn them). Then just concat strings adding spaces as needed.
String.Split() is an amazing tool. You will benefit greatly by learning it.
Upvotes: 4