无法使用HashMaps解码莫尔斯电码。

时间:2016-12-14 03:19:17

标签: java object hashmap morse-code

我的代码在将常规文本编码为摩尔斯电码时效果很好,但是当我尝试翻译另一种方式时,我遇到索引越界错误。不明白为什么?

import java.util.HashMap;
public class MorseCode {
private final String alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
private final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
        ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
        "-....", "--...", "---..", "----.", "-----", "|"};
private HashMap<String, String> toText;
private HashMap<String, String> toCode;

public MorseCode() {
    toText = new HashMap<>();
    toCode = new HashMap<>();
    char[] alphaArray = alphabet.toCharArray();
    for(int i = 0; i < morse.length; i++) {
        toCode.put(morse[i], String.valueOf(alphaArray[i]));
        toText.put(String.valueOf(alphaArray[i]), morse[i]);
    }

}

public String encode(String s) {
    s = s.toLowerCase();
    String encoded = "";
    char[] chars = s.toCharArray();
    for(int i = 0; i < s.length(); i++) {
        for (HashMap.Entry<String, String> entry : toCode.entrySet()) {
            if (String.valueOf(chars[i]).equals(entry.getValue())) {
                encoded += entry.getKey() + " ";
            }
        }
    }

    return encoded;
}

public String decode(String s) {
    s = s.toLowerCase();
    String decoded = "";
    for(int i = 0; i < s.length(); i++) {
        for (HashMap.Entry<String, String> entry : toText.entrySet()) {
            if (morse[i].equals(entry.getValue())) {
                decoded += entry.getKey();
            }
        }
    }

    return decoded;
}

}

尝试找到兼顾两种方式的解决方案,我们将不胜感激任何帮助/建议!

1 个答案:

答案 0 :(得分:0)

我测试了这段代码并且它正在运行。请注意,这是一个非常基本的实现,但正确使用了HashMap类的一些便利。我强烈建议您在调试模式下运行并跟踪变量的值。

import java.util.HashMap;

public class MorseCode {

    //Constants hold the accepted characters
    private final String alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
    private final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
        ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
        "-....", "--...", "---..", "----.", "-----", "|"};

    //Maps map the code to text and text to code
    private HashMap<String, String> toText;
    private HashMap<String, String> toCode;

    //Problem: toText had text as keys. Keys should be used to identify the value your want to get.
    //Solution: swapped toText logic to toCode logic
    public MorseCode() {
        toText = new HashMap<>();
        toCode = new HashMap<>();
        char[] alphaArray = alphabet.toCharArray();
        for(int i = 0; i < morse.length; i++) {
            toText.put(morse[i], String.valueOf(alphaArray[i]));
            toCode.put(String.valueOf(alphaArray[i]), morse[i]);
        }
    }

    //Problem: kind of complicated logic. Was working, but did not need all that truncation
    //Solution: HashMap contains neat methods to work with keys - get(key) and containsKey(key)
    //In this solution, if the key is not found, we print the plain text character
    public String encode(String s) {
        s = s.toLowerCase();
        String encoded = "";
        for(int i = 0; i < s.length(); i++) {
            String c = String.valueOf(s.charAt(i));
            if (toCode.containsKey(c)) {
                encoded += toCode.get(c) + " ";
            } else {
                encoded += c;
            }
        }
        return encoded;
    }

    //Problem: logic was broken. Again, you are mapping the key to the value you want in toText, so use it.
    //Solution: Same logic than the encode method, but we had to strip off the spaces
    public String decode(String s) {
        String[] code = s.split(" ");
        String decoded = "";
        for(int i = 0; i < code.length; i++) {
            if (toText.containsKey(code[i])) {
                decoded += toText.get(code[i]);
            } else {
                decoded += "?";
            }
        }
        return decoded;
    }
}

主方法中的测试代码。此测试应该真正采用所有边界情况(特殊字符,多个空格,空字符串等),并将输入与预期输出进行比较。但现在为时已晚。 : - )

public class Main {
    public static void main(String[] args) {
        MorseCode m = new MorseCode();
        String encoded = m.encode("Unencoded Text");
        String decoded = m.decode("..- -. . -. -.-. --- -.. . -.. | - . -..- - ");
        System.out.println(encoded);
        System.out.println(decoded);

    }
}

干杯。