使用Java中的HashMaps进行摩尔斯电码转换器

时间:2014-08-08 04:44:40

标签: java collections hashmap

我确实已经意识到这个问题已被提出,我已经通过他们寻求帮助。我正在尝试编写一个莫尔斯电码转换器。这可以翻译字母和数字;我已经让英语到莫尔斯使用HashMap完美地工作了。 Google Gods告诉我,我可以颠倒hashmap的顺序,以便将莫尔斯语翻译成英文,但我正在画一个空白。

我正在寻找的是一种在莫尔斯电码中输入字符串的方法,包括空格并获取英文翻译,包括数字?

import java.util.*;

public class Project {
    public static void main(String[] args) {
        getInput();
    }

    public static void getInput() {
        char a;
        a = Input.getChar("Enter m for Morse to English or e for English to Morse.");
        if (a == 'm')
            translateMorse();
        else if (a == 'e')
            translateEnglish();
        else 
            System.out.println("Your input is not valid.");
    }

    public static void translateEnglish() {
        String englishToMorse = Input.getString("Enter a phrase in English.");
        englishToMorse = englishToMorse.toLowerCase();

        int i = 0;

        while(i <englishToMorse.length()) {
            Map<Character, String> morse = new HashMap<Character, String>();
            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");
            System.out.print(morse.get(englishToMorse.charAt(i)));
            i++;
        }
    }

    public static void translateMorse() {  
        String morseToEnglish = Input.getString("Enter a phrase in Morse Code.");
        morseToEnglish = morseToEnglish.toLowerCase();

        int i = 0;
        while(i < morseToEnglish.length() ) {
            HashMap<Character, String> morse = new HashMap<>();
            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");

            Collection<String>  values= morse.values();

            for (String string: values) {
                System.out.print(string);
            }
            i++;
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

您可以通过迭代HashMap来反转EntrySet,如下所示:

Map<String, Character> morseToEngMap = new HashMap<>();
// engToMorseMap is your original hashmap object named morse.
// morseToEngMap is reversed map
for(Map.Entry<Character, String> entry : engToMorseMap.entrySet()){ 
    morseToEngMap.put(entry.getValue(), entry.getKey());
} 

或者,您可以使用具有Guava library方法的inverse()为您完成工作。

BiMap<Character, String> engToMorseMap = HashBiMap.create(); 
// engToMorseMap is your original hashmap object named morse.
morse.put('a', ".-");
morse.put('b', "-...");

BiMap<String, Character> morseToEngMap = morse.inverse();
// morseToEngMap is reversed map.

希望这会对你有所帮助。

答案 1 :(得分:0)

谢谢你Shishir Kumar的快速反应,我昨晚看到了,但是很累。您发布的方式是我在寻求帮助之前尝试过的一种方式。如果我违反本论坛的规则,我会道歉,但这是我第一次寻求帮助。这是我试过的另一个代码版本,我更改了变量名称以匹配你的。

import java.util.*;

公共类项目{

    public static void main(String[] args)
    {


        getInput();


    }

    public static void getInput()
        {

        char a;
        a = Input.getChar("Enter m for Morse to English or e for English to Morse.");
        if (a == 'm')
            translateMorse();
        else if (a == 'e')
            translateEnglish();
        else 
            System.out.println("Your input is not valid.");
        }

    public static void translateEnglish()
        {



        String englishToMorse = Input.getString("Enter a phrase in English.");
        englishToMorse = englishToMorse.toLowerCase();

        int i = 0;

        while(i <englishToMorse.length() ){

            Map<Character, String> morse = new HashMap<Character, String>();

            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");

            System.out.print(morse.get(englishToMorse.charAt(i)));

            i++;

            }


        }

    public static void translateMorse()
        {  

        String morseToEnglish = Input.getString("Enter a phrase in Morse Code.");
        morseToEnglish = morseToEnglish.toLowerCase();

        int i = 0;

        while(i < morseToEnglish.length() ){

            HashMap<Character, String> morse = new HashMap<>();

            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");

          Map <String, Character> englishMap = new HashMap<>();

          int k = 0;

          for (Map.Entry<Character, String> entry : morse.entrySet())
          {
              k++;

              System.out.println("This is the for loop " + k);
              englishMap.put(entry.getValue(), entry.getKey());

          }

          System.out.println(morse.get(morseToEnglish.charAt(i)) + "\nTHis is I = " + i);

          System.out.print(morseToEnglish);

            i++;


            }
        }



    }

我得到的唯一输出是null,我知道或者至少认为我做的事情非常愚蠢。