Translate morse code into alphabets

浪尽此生 提交于 2019-12-25 01:48:54

问题


I am working on a project that could translate morse code into english and vice versa. The below is the specific instruction: "Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results. When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. For example, - --- | -... . would be the Morse Code input for the sentence “to be”. Your program only needs to handle a single sentence and can ignore punctuation symbols."

Although I figured out how to translate english to morse code, I cannot figure out how to translate morse code to english. And if you are reading this please help me! Any help or hints would be very much appreciated! Thanks :)

public static String[] morse = { ".- ", "-... ", "-.-. ", "-.. ", ". ",
        "..-. ", "--. ", ".... ", ".. ",

        ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ",
        ".-. ", "... ", "- ", "..- ",

        "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "|" };

public static String[] alphabet = { "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", " " };

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Input '1' to translate English to Morse Code.");
    System.out.println("Input '2' to translate Morse Code to English.");
    int kind = in.nextInt();

    if (kind == 1) {
        System.out.println("Please insert an alphabet string");
        String Eng = in.next();
        EngToMo(Eng);
    }
    if (kind == 2) {
        System.out.println("Please insert a morse string");
        String Mor = in.next();
        MoToEng(Mor);
    }

}

public static void EngToMo(String string1) {
    String Upper1 = string1.toUpperCase();
    for (int i = 0; i < Upper1.length(); i++) {
        char x = Upper1.charAt(i);
        if (x != ' ') {
            System.out.print(morse[x - 'A'] + " ");
        } else {
            System.out.println(" | ");
        }
    }
}

public static void MoToEng(String string2) {

    }
}

回答1:


I suggest to create a dictionary using Hashtable, where Alphabets can be used as Key and relevant Morse Code can be paired with this key. If you want to have unique Key Value pair you can use BiMap for storage.

Map<String, String> codeMap = new HashMap<String, String>();
codeMap.put("A", ".- ");
codeMap.put("B", "-... ");

you can easily access this map to get either keys or values

for (String key: codeMap.keySet() ){
    System.out.println(key+" : "+codeMap.get(key) );
}



回答2:


you can split the input string, then parse it's content...

public static void MoToEng(String string2) {
    String[] splits = string2.split(" "); //you split the string into
                                           //String[] - delimiter = ' '


    String literal = ""; //this will be your result

    for (String split: splits){ //iterate through the String[] split

        splits = splits + " "; //your morse code ends with
                               //a blank - you have to add that 
                               //else equals() won't find 
                               //a suitable match
        //then look into your morse code
        for (int index = 0; index < morse.length(); index++){ 
            string code = morse[index];

            //if split looks like a morse code, you found your 
            //character
            if (split.equals(code){

                //translate back then
                literal = literal + ('A'+index);

            }
        }
    }
    System.out.println(literal);
}



回答3:


Why dont you use a Map<String,String> for storing <morsecode,english alphabet> values ?

HashMap<String,String> map=new HashMap<String,String>();
//populate map
map.put(".- ","a");
map.put("-... ","b");

and then in the method EngToMo(String string1)

public static void EngToMo(String string1){

for (Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue().equals(string1)) {
                System.out.println(entry.getKey());
            }
        }
}

and in the method MoToEng(String string2)

public static void MoToEng(String string2){
 System.out.println(map.get(string2));
}


来源:https://stackoverflow.com/questions/28018666/translate-morse-code-into-alphabets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!