简体   繁体   中英

Need assistance with java morse code translator

I have a Java translator which works when I translate from English to Morse but not from Morse to English. If you could tell me what I need to do to make it translate morse that would be great. When I go from morse to english after I enter my morse code it just ends the program instead of giving me the translation.

Here is my code.

public class project1 {

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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    String a = Input.getString ( "Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code" );
if (a.equals("MC"))
    {
        String b = Input.getString ("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String[] words = b.split("|");
        for (String word: words )
        {
            String[] characters = word.split(" ");
            for (String character: characters) 
            {
                if (character.isEmpty()) { continue; }
        for (int m = 0; m < b.length(); m++)
                {
                    if (character.equals("inputMorseCode[m]"))    
                        System.out.print(english[ m ]);    
                }    
            }
            System.out.print(" ");    
        }    
    }
else if (a.equals("Eng"))
    {
        String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );

        c = c.toLowerCase ();

        for ( int x = 0; x < english.length; x++ )
        {
            for ( int y = 0; y < c.length (); y++ )
            {
                if ( english [ x ] == c.charAt ( y ) )

                System.out.print ( morse [ x ] + "  " );


            }

        }


    }

    else 
   {
       System.out.println ( "Invalid Input" );

    }

}
}
  1. Your counter is on the wrong thing: for (int m = 0; m < b.length(); m++) should be for (int m = 0; m < morse.length; m++) so you're limited by the number of characters in the alphabet, not the number of characters input by the user.
  2. You're not comparing the character to a morse character, you're comparing it to the string "inputMorseCode[m]" . Change if (character.equals("inputMorseCode[m]")) to if (character.equals(morse[m]))

First of all, change this

    for (int m = 0; m < b.length(); m++)
            {
                if (character.equals("inputMorseCode[m]"))    
                    System.out.print(english[ m ]);    
            }  

to

    for (int m = 0; m < morse.length; m++)
            {
                if (character.equals(morse[m]))    
                    System.out.print(english[m]);    
            } 

Since you are supposed to search the morse letter in the morse array.

That said, your code would be much more efficient if you create a Map<String,Character> and Map<Character,String> that Map morse Strings to English Characters and vice versa. They would replace your morse and english arrays, and will allow you to find the mapping of an English or Morse letter in constant time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM