-2

So I finally managed to fix and finish my source code for my own Morse Code translator, from english to morse code and vice-versa. The english to morse code works perfectly no problems, but the morse code to english works with only the input but it doesn't display the english after it is translated. As I said, there are no errors, it compiles fine it just doesn't display. Here is my code and I you guys could help me realize what is wrong, I would be extremely greatful. Thanks

public class Project1
{
public static void main( String [] args )
{
    System.out.println();
    choice();

}
    public static void choice()
{
    int user_choice = 0; 
    user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
    if(user_choice == 1)
    {
    String output = new String();
    String inital = new String();
    inital = english_to_morse();

    for( int k = 0; k < inital.length(); k++)
    {
        output += morse(inital.charAt( k ));
    }

        System.out.print(output);

    }
    if(user_choice == 2)
    {
    String output2 = new String();
    String inital2 = new String();
    inital2 = morse_to_english();

    for( int k = 0; k < inital2.length(); k++)
    {
        output2 += english(String.valueOf(inital2.charAt( k )));
    }
        System.out.print(output2); ///This is where the display is
    }
}

public static String english_to_morse() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");

  return user_input.toLowerCase();
}

public static String morse_to_english() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");

  return user_input.toLowerCase();
}

public static String morse(char letter)
{
    String output = new String();
    char[] alphabet_numbers = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < alphabet_numbers.length; j++ )
    {
        if (alphabet_numbers[j]==letter)
        {
            output = morse_code[j];
        }
    }
    return output + " ";
}   
public static String english(String letter)
{
    String output = new String();
    String alphabet_numbers[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < morse_code.length; j++ )
    {
        if (morse_code[j]==letter)
        {
            output = alphabet_numbers[j];
        }
    }
    return output + " ";
}   
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57

1 Answers1

1

There is a fair number of things to be improved, but the most important one would probably be your String comparison in english(...): If you compare two Strings using == as in

if (morse_code[j]==letter)

this will only yield true if the object references are the same, not the strings. You should use

if (morse_code[j].equals(letter))

this will greatly improve your chances of finding any characters (which you obviously do not at the moment, you should consider printing some error message if a .- series cannot be found in the morse_codes).

Patru
  • 4,481
  • 2
  • 32
  • 42
  • I did that and it displays the word but when i put together multiple dots and dashes, it just display the english letter for a single dot or single dash – user3474526 Mar 29 '14 at 12:30
  • @user3474526 You do realise that in Morse code between 1 and 3 characters are converted into a single English letter? Your conversion uses `inital2.charAt(k)`. How do you expected this to work? – Boris the Spider Mar 29 '14 at 12:38
  • Hmmh, maybe your "encoding" of morse code is incomplete? There is a pause in between letters in morse code in order to determine when a letter is finished. You would have to "tokenize" your morse "string" in to split it into morse letters. – Patru Mar 29 '14 at 22:17