将英语翻译成摩尔斯电码JAVA的程序

时间:2015-03-03 23:49:12

标签: java

我在一个应该将英语翻译成摩尔斯电码的程序上遇到麻烦。

public class MorseCode
 {
  private static 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"};
  private static ArrayList<String> morse = new ArrayList<String> ();
  public MorseCode(String fileName) throws IOException
{
    Scanner inFile = new Scanner(new File(fileName));
     while(inFile.hasNext()){
         morse.add(inFile.next());
    }
        }

    public static void toMorse(String a)
    {
        String [] phrase = a.split("");  
        String translated = "";
        for (String b : phrase)
        {
            for (int i = 0; i < 36; i++)
            {
                if (b == english[i])
                {
                 translated += morse.get(i);
                 System.out.println(translated);
                }

            }


        }

        System.out.println(translated);
    }

}

基本上,我在String array [] English中有一组英文字母/数字。 Array List morse包含从文件中读取的莫尔斯字符列表。英语数组和莫尔斯数组列表的长度相同,并且在翻译方面相对应。

在toMorse函数中,我有一个String数组短语[],它包含要翻译的用户输入消息的每个单独字母。 对于消息中的每个字母(短语[]中的每个索引),我会浏览字母表中的每个字母(english []),当我找到匹配项时,我会使用相同的匹配索引,并附加莫尔斯字符将该索引转换为字符串。

我没有看到任何错误,但是当我运行程序时,在我输入要翻译的消息后,只有空格。我无法将结果打印出来。我确保大多数都能正常工作,但我认为for和if语句有问题。

我有一个单独的类来运行它:

public class MorseCodeTester
{
   public static void main (String args[]) throws IOException
{
    MorseCode m = new MorseCode ("morsecode.txt");
    Scanner in = new Scanner (System.in);
    System.out.println("Enter a message to translate to Morse Code: ");
    String message = in.next();
    message = message.toLowerCase();
    m.toMorse(message);
}

}

非常感谢任何帮助。谢谢!

0 个答案:

没有答案