Convert Morse code to English text using Java

Recently, I was commissioned to convert English into Morse code and Morse code into English. When entering the Morse code, my teacher wants the individual letters to be separated by 1 space and the words to be separated by the '|' sign. For example - --- | -... should be. "I was able to work perfectly with the British before Morse, but I can’t understand that Morse is in English. I don’t know how to make the for loop stop at the right point and match it with one of the codes in the array .

Just a note, my teacher doesn't like the Scanner, so he uses his own input system. I am familiar with its methods, so avoid using the Scanner.

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) );

            }
        }   
    }
}

. , , morseToEnglish , .

+4
2

String.split(). |, . toString(), , ( , ). , .

String.Split() - . , .

+4

MorseToEnglish englishToMorse.

, . , . . , . , , , - (- HashTable). , , , , , .

+2

All Articles