3

My problem right now is getting the "morse code to english" to work. the first part which converts english to morse is working perfectly. i know this has been asked before but i can't figure out what i am doing wrong. i know i need a split somewhere, but I'm just not sure where to put it in the code. right now the morse to english part won't even convert one symbol. any help will be much appreciated.

import java.util.Scanner;

public class MorseCode2  {
    public static void main ( String [] args )
    {
        Scanner input = new Scanner( System.in );
        System.out.print( "Would you like to convert English to Morse (yes or no)? " );
        String answer = input.nextLine();
        if( answer.equals( "yes" ) )
        {
        System.out.println( "Please enter the text you want to translate into Morse code: " );
        String english = input.nextLine();
        System.out.println( stringToMorse( english ) );
        }

        if (answer.equalsIgnoreCase( "no" ) )
        {
            System.out.print( "Morse to English? " );
            String answer2 = input.nextLine();
            if (answer2.equalsIgnoreCase( "yes" ) )
            {
                System.out.println( "Please enter the Morse code you want to translate into English: " );
                String code = input.nextLine();
                System.out.println( stringToEnglish( code ) );
            }
        }
    }

    public static String encode (String toEncode)
    {
        String morse = toEncode;

        if (toEncode.equalsIgnoreCase("a"))
            morse = ".-";
        if (toEncode.equalsIgnoreCase("b"))
            morse = "-...";
        if (toEncode.equalsIgnoreCase("c"))
            morse = "-.-.";
        if (toEncode.equalsIgnoreCase("d"))
            morse = "-..";
        if (toEncode.equalsIgnoreCase("e"))
            morse = ".";
        if (toEncode.equalsIgnoreCase("f"))
            morse = "..-.";
        if (toEncode.equalsIgnoreCase("g"))
            morse = "--.";
        if (toEncode.equalsIgnoreCase("h"))
            morse = "....";
        if (toEncode.equalsIgnoreCase("i"))
            morse = "..";
        if (toEncode.equalsIgnoreCase("j"))
            morse = ".---";
        if (toEncode.equalsIgnoreCase("k"))
            morse = "-.-";
        if (toEncode.equalsIgnoreCase("l"))
            morse = ".-..";
        if (toEncode.equalsIgnoreCase("m"))
            morse = "--";
        if (toEncode.equalsIgnoreCase("n"))
            morse = "-.";
        if (toEncode.equalsIgnoreCase("o"))
            morse = "---";
        if (toEncode.equalsIgnoreCase("p"))
            morse = ".--.";
        if (toEncode.equalsIgnoreCase("q"))
            morse = "--.-";
        if (toEncode.equalsIgnoreCase("r"))
            morse = ".-.";
        if (toEncode.equalsIgnoreCase("s"))
            morse = "...";
        if (toEncode.equalsIgnoreCase("t"))
            morse = "-";
        if (toEncode.equalsIgnoreCase("u"))
            morse = "..-";
        if (toEncode.equalsIgnoreCase("v"))
            morse = "...-";
        if (toEncode.equalsIgnoreCase("w"))
            morse = ".--";
        if (toEncode.equalsIgnoreCase("x"))
            morse = "-..-";
        if (toEncode.equalsIgnoreCase("y"))
            morse = "-.--";
        if (toEncode.equalsIgnoreCase("z"))
            morse = "--..";
        if (toEncode.equalsIgnoreCase("0"))
            morse = "-----";
        if (toEncode.equalsIgnoreCase("1"))
            morse = ".----";
        if (toEncode.equalsIgnoreCase("2"))
            morse = "..---";
        if (toEncode.equalsIgnoreCase("3"))
            morse = "...--";
        if (toEncode.equalsIgnoreCase("4"))
            morse = "....-";
        if (toEncode.equalsIgnoreCase("5"))
            morse = ".....";
        if (toEncode.equalsIgnoreCase("6"))
            morse = "-....";
        if (toEncode.equalsIgnoreCase("7"))
            morse = "--...";
        if (toEncode.equalsIgnoreCase("8"))
            morse = "---..";
        if (toEncode.equalsIgnoreCase("9"))
            morse = "----.";
        if (toEncode.equalsIgnoreCase("."))
            morse = ".-.-";
        if (toEncode.equalsIgnoreCase(","))
            morse = "--..--";
        if (toEncode.equalsIgnoreCase("?"))
            morse = "..--..";

        return morse;
    }

    public static String decode (String toEncode) {
        String morse = toEncode;

        if (toEncode.equalsIgnoreCase(".-"))
            morse = "a";
        if (toEncode.equalsIgnoreCase("-..."))
            morse = "b";
        if (toEncode.equalsIgnoreCase("-.-."))
            morse = "c";
        if (toEncode.equalsIgnoreCase("-.."))
            morse = "d";
        if (toEncode.equalsIgnoreCase("."))
            morse = "e";
        if (toEncode.equalsIgnoreCase("..-."))
            morse = "f";
        if (toEncode.equalsIgnoreCase("--."))
            morse = "g";
        if (toEncode.equalsIgnoreCase("...."))
            morse = "h";
        if (toEncode.equalsIgnoreCase(".."))
            morse = "i";
        if (toEncode.equalsIgnoreCase(".---"))
            morse = "j";
        if (toEncode.equalsIgnoreCase("-.-"))
            morse = "k";
        if (toEncode.equalsIgnoreCase(".-.."))
            morse = "l";
        if (toEncode.equalsIgnoreCase("--"))
            morse = "m";
        if (toEncode.equalsIgnoreCase("-."))
            morse = "n";
        if (toEncode.equalsIgnoreCase("---"))
            morse = "o";
        if (toEncode.equalsIgnoreCase(".--."))
            morse = "p";
        if (toEncode.equalsIgnoreCase("--.-"))
            morse = "q";
        if (toEncode.equalsIgnoreCase(".-."))
            morse = "r";
        if (toEncode.equalsIgnoreCase("..."))
            morse = "s";
        if (toEncode.equalsIgnoreCase("-"))
            morse = "t";
        if (toEncode.equalsIgnoreCase("..-"))
            morse = "u";
        if (toEncode.equalsIgnoreCase("...-"))
            morse = "v";
        if (toEncode.equalsIgnoreCase(".--"))
            morse = "w";
        if (toEncode.equalsIgnoreCase("-..-"))
            morse = "x";
        if (toEncode.equalsIgnoreCase("-.--"))
            morse = "y";
        if (toEncode.equalsIgnoreCase("--.."))
            morse = "z";
        if (toEncode.equalsIgnoreCase("-----"))
            morse = "0";
        if (toEncode.equalsIgnoreCase(".----"))
            morse = "1";
        if (toEncode.equalsIgnoreCase("..---"))
            morse = "2";
        if (toEncode.equalsIgnoreCase("...--"))
            morse = "3";
        if (toEncode.equalsIgnoreCase("....-"))
            morse = "4";
        if (toEncode.equalsIgnoreCase("....."))
            morse = "5";
        if (toEncode.equalsIgnoreCase("-...."))
            morse = "6";
        if (toEncode.equalsIgnoreCase("--..."))
            morse = "7";
        if (toEncode.equalsIgnoreCase("---.."))
            morse = "8";
        if (toEncode.equalsIgnoreCase("----."))
            morse = "9";
        if (toEncode.equalsIgnoreCase("|"))
            morse = "";

        return morse;
    }

    public static String stringToMorse( String text )
    {

        String newText = "";
        String selectedChar;
        String convertedChar;
        for (int i = 0; i < text.length(); i++)
        {

            //Select the next character
            selectedChar = text.charAt(i) + "";

            // Convert the character
            convertedChar = encode(selectedChar);

            if (convertedChar.equals(" ")) // " " separates each word represented in english code
            {
                newText = newText + "| ";
            }
            // Add the converted text, and add a space
            else
            {
                newText = newText + convertedChar;
                if (!convertedChar.equals(" "))
                {
                    newText = newText + " ";
                }
            }
        }

        return newText;
    }

    public static String stringToEnglish( String text )
    {
       String newMorse = ""; //sets string for newMorse
       String selectedMorse; //sets string for selectedMorse
       String convertedMorse; //sets string for convertedMorse
       for (int i = 0; i < text.length(); i++)
       {

           //Select the next character
           selectedMorse = text.charAt(i) + "";

           // Convert the character
           convertedMorse = encode(selectedMorse);

           if (convertedMorse.equals("| ")) // "|" separates each word represented in morse code
           {
               newMorse = newMorse + " ";
           }
           // Add the converted text, and add a space
           else
           {
               newMorse = newMorse + convertedMorse;
               if (!convertedMorse.equals(" "))
               {
                   newMorse = newMorse + " ";
               }
           }
       }

       return newMorse;
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Cfs0004
  • 85
  • 2
  • 14
  • You might want to consider something like prefix-based matching if you don't have spaces in your morse code. Consume a character, perform a lookup in something like a trie, consume next until match. I'm not sure if morse code can have a mismatch of sorts where consuming a prefix too greedily will cause a mismatch later. If so you might need to do backtracking. – nanofarad Jun 17 '15 at 22:46
  • To fix your immediate problem, you should probably call `decode(selectedMorse);` instead of `encode` in `stringToEnglish`. Also, you cannot use `text.charAt(i)` there, because a single morse code consists of multiple characters. – Mick Mnemonic Jun 17 '15 at 22:49
  • 1
    Morse code, without some delimiter between character codes is ambiguous. See http://cs.stackexchange.com/questions/34067/is-morse-code-without-spaces-uniquely-decipherable – hatchet - done with SOverflow Jun 17 '15 at 22:53

1 Answers1

2

You probably want something like this:

public static String stringToEnglish( String text )
{
   String newEnglish = ""; //sets string for newEnglish
   String selectedEnglish; //sets string for selectedEnglish
   String convertedEnglish; //sets string for convertedEnglish
   String[] morseChars = text.split(" ");
   for (int i = 0; i < morseChars.length; i++)
   {
       //Select the next Morse character
       selectedEnglish = morseChars[i];
       boolean endsWithWordSeparator = selectedEnglish.endsWith("|");
       if(endsWithWordSeparator) selectedEnglish = selectedEnglish.substring(0, selectedEnglish.length() - 1);
       // Convert the string
       convertedEnglish = decode(selectedEnglish);

       newEnglish = newEnglish + convertedEnglish;
       if (endsWithWordSeparator) 
       {
           newEnglish = newEnglish + " ";
       }
   }

   return newEnglish;
}
Diego
  • 18,035
  • 5
  • 62
  • 66
  • Never mind this works perfectly. do you mind explaining what you did for my learning purposes? – Cfs0004 Jun 18 '15 at 01:45
  • Morse codes are sets of 2-5 chars separated by spaces, so split the string by spaces first, then decode each, but first, take out the `|` at the end if any, which indicates a word break. That's it. – Diego Jun 18 '15 at 20:24