需要帮助java morse代码翻译只翻译一个符号

时间:2015-05-30 20:15:17

标签: java morse-code

我刚刚创建了一个java morse代码翻译器,但是当我将莫尔斯转换为英语时,它将每个符号转换为字母,所以当我翻译时......它给了我e e e e而不是h。所以我的问题是如何制作它,以便我可以翻译摩尔斯电码中的整个单词并使用|分开单词。

这是我的代码

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 < morse.length; m++)
                {
                    if (character.equals(morse[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" );

    }

}
}

2 个答案:

答案 0 :(得分:0)

您遇到的问题是String#split(...)方法没有考虑|是关于正则表达式的特殊字符,并且没有像您期望的那样拆分字符串。你需要逃避这个角色以使事情正常工作。所以改变这个:

String[] words = b.split("|");

到此:

String[] words = b.split("\\|"); // escaping the pipe char

要调试并查看事情是否按预期工作,请使用调试器或在代码中添加println,如:

 String[] words = b.split("\\|");

 // TODO: remove line below from finished program
 System.out.println(java.util.Arrays.toString(words)); 

顺便说一句,如果这是我的计划,我会使用HashMap<String, String>协助我将英语翻译成莫尔斯语,反之亦然。

在进一步搜索我刚刚执行的操作时,请查看:Why does String.split need pipe delimiter to be escaped?以获取有关管道char需要转义的原因的背景信息。

答案 1 :(得分:-1)

这是你改编的代码,它可以正常工作......

import java.util.Scanner;

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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code");
String a = scanner.nextLine();

if (a.equals("MC"))
    {
        System.out.println("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String b = scanner.nextLine();

        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 < morse.length; m++)
                {
                    if (character.equals(morse[m]))    
                        System.out.print(english[m]);    
                }    
            }
            System.out.print(" ");    
        }    
    }
else if (a.equals("Eng"))
    {
        System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
        String c = scanner.nextLine();

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

    }

}
}