简体   繁体   English

摩尔斯电码应用程序(JAVA Gui)

[英]Morse Code Application (JAVA Gui)

I am attempting to create a gui application that reads english user input and translates to morse ( http://ascii-table.com/morse-code.php ). 我正在尝试创建一个gui应用程序,读取英文用户输入并转换为莫尔斯( http://ascii-table.com/morse-code.php )。 I have touched on the basic parts of the program so far. 到目前为止,我已经触及了该计划的基本部分。 My question is; 我的问题是; what is the best way of going about reading morse? 阅读莫尔斯的最佳方式是什么? should i create a text file to import the morse letters off or should i declare each of them inside the program to translate? 我应该创建一个文本文件来导入莫尔斯字母,还是我应该在程序中声明每个字母进行翻译? Next question is how would i go about doing this? 接下来的问题是我将如何做到这一点? please refer to a tutorial if possible. 如果可能请参考教程。 Thanks for your time. 谢谢你的时间。

Since Morse Code is not likely to change, hard-coding the mapping of characters to code strings is a valid option: 由于摩尔斯电码不太可能改变,因此对字符到代码字符串的映射进行硬编码是一个有效的选择:

private static Map<Character,String> charToCode = new HashMap<Character,String>();
{
    charToCode.put('A', ".-");
    charToCode.put('B', "-...");
    ...
    charToCode.put('Z', "--..");
}

This map lets you convert messages to code one character at a time: 此映射允许您将消息转换为一次编码一个字符:

  • Make a StringBuilder for the result 为结果创建一个StringBuilder
  • Go through characters of the input one character at a time. 一次浏览输入一个字符的字符。 You can use charAt(i) for that 您可以使用charAt(i)
  • Convert the character to upper case 将字符转换为大写
  • Use charToCode.get(upperChar) to look up the code representation of the character 使用charToCode.get(upperChar)查找字符的代码表示
  • Append the representation to the StringBuilder ; 将表示附加到StringBuilder ; append a space after it 在它之后添加一个空格
  • Once the loop is over, convert StringBuilder to String , and put it on the label. 循环结束后,将StringBuilder转换为String ,并将其放在标签上。

You can have two files maintained outside. 您可以在外部维护两个文件。 One having a mapping from Letters to Morse Code, and other from Morse Code to Letters. 一个有从Letters到Morse Code的映射,还有从Morse Code到Letters的映射。 And then, you can build both the converters by doing a lookup from the corresponding file. 然后,您可以通过从相应文件中进行查找来构建转换器。

Either you can Read the Input from the User and compare them while Run time to the Predefined characters and Numbers Stored in the Program Or you can Input a File and Read it using FileReader 您可以从用户读取输入并在运行时将它们与程序中存储的预定义字符和数字进行比较或者您可以使用FileReader输入文件并读取它

But Here is the Logic that you are Supposed to Perform for Run Time Comparison 但这是您要为运行时比较执行的逻辑

 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',
             ',', '.', '?' };   //Defining a Character Array of the English Letters numbers and Symbols so that we can compare and convert later 

     String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                "-----", "--..--", ".-.-.-", "..--.." };

System.out.println("-->Enter the Sentence that you want to Transmit Using the Morse Code ");
        System.out.print("->");
        sentence = br.readLine();
        System.out.println("");
        sentence = sentence.toLowerCase(); //Because morse code is defined only for the lower case letters and the numbers and the Symbols will remain the Same
        char[] morsec = sentence.toCharArray();
        for(int i = 0; i < morsec.length;i++)  //The loop will run till i is less than the number of characters in the Sentence because Every Character needs to Be Converted into the Respective Morse Code 
        {//For Every Letter in the User Input Sentence
            for(int j = 0;j<english.length;j++)   //For Every Character in the morsec array we will have to traverse the entire English Array and find the match so that it can be represented 
            {
                if(english[j] == morsec[i])  //If the Character Present in English array is equal to the character present in the Morsec array then Only Execute 
                {//Always remember that the condition in the Inner loop will be the first to be Equated in the If Statement because that will change until the characters match 
                    answer = answer + morse[j] + " ";  //After Every Letter is generated in the Morse Code we will give a Space 
                }  //Since the Letters in the English char and the symbols present in the morse array are at the Same Index 
            }
        }
        System.out.println("-->The Morse Code Translation is:- ");
        System.out.Println(answer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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