需要协助与java莫尔斯电码翻译器只翻译一个符号



我刚刚做了一个java莫尔斯电码翻译器,但是当我把莫尔斯转换成英语时,它把每个符号转换成一个字母,所以当我把它翻译成....它给了我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" );
    }
}
}

您遇到的问题是您的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>来帮助我将英语翻译成莫尔斯码,反之亦然。

关于我刚刚执行的进一步搜索,请查看:为什么字符串。分割需要转义管道分隔符吗?有关管道字符需要转义的背景信息

这是您改编的代码,可以正常工作…

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

最新更新