Search code examples
javamorse-code

Morse code translator(simple)


I am working on a simple Morse Code translator for my Intro to Programming class. This is a very simple design based on the techniques I have been taught.

This program works for a single character conversion, but cannot do words or sentences. I believe the problem has to do with the morse[index] statement at the end, but I can't figure out how to print the translated text as a whole.

public class Exercise12_9
{
    public static void main(String[] args)
    {
        String[] english = { "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
                  ",", ".", "?" };

        String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                "-----", "--..--", ".-.-.-", "..--.." };


        Scanner keyboard = new Scanner(System.in);

        String userInput;

        int index;

        index = 0;

        System.out.println(" This is an English to Morse Code Translator.  ");
        System.out.println(" Please enter what you would like translate ");
        System.out.println("             into Morse Code. ");
        System.out.println(" ============================================ ");

        userInput = keyboard.next();

        userInput = userInput.toLowerCase();

        for (index = 0; index < userInput.length(); index++)           
        {
            char [] chars = userInput.toCharArray();

            if (userInput.equals(english[index]))
            {    
                System.out.println(" Translated : " + morse[index]);       
            }
        }  
    }
}

Solution

  • I believe you are trying to achieve something like this. You were on a good path, although you need to take a look at a few pointers I have for your code.

    1. first of all you created an array of String for the alphanumeric in english. Since you take an input from user and split it in char, you should have created an array of char instead. Since you were trying to compare user input with your array, you were using something.equals(something else) --> this is a method for String.. Now that you have two character to compare use the == comparision sign.
    2. It is good practice to initiate a variable and declare it's starting value on the same line (less line of code). Same thing goes for the for loop, initiate the index variable directly in the loop declaration. (Usually starting with letter i instead of the variable index).
    3. The double for loop at the end is necessary to compare every character from the input to every character of your english letters and numbers.
    4. For the answer suggested below, I used a String str to concatenate the morse value. Then you simply have to print out it's value.

    Although I modified a bit your code, play around with with and see the different outputs it creates, this is the best way to learn from the code given.

    Good luck with the learning

    public static void main(String[] args){
    
        char[] english = { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                      ',', '.', '?' };
    
        String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                    ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                    "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                    "-----", "--..--", ".-.-.-", "..--.." };
    
        //Scanner keyboard = new Scanner(System.in);
    
        System.out.println(" This is an English to Morse Code Translator.  ");
        System.out.println(" Please enter what you would like translate ");
        System.out.println("             into Morse Code. ");
        System.out.println(" ============================================ ");
    
        //String userInput = keyboard.nextLine().toLowerCase();
        String userInput = "TEST".toLowerCase();
    
        char[] chars = userInput.toCharArray();
    
        String str = "";
        for (int i = 0; i < chars.length; i++){
            for (int j = 0; j < english.length; j++){
    
                if (english[j] == chars[i]){
                    str = str + morse[j] + " ";  
                }
            }
        }
        System.out.println(str);
    }