String.equals-方法在比较两个字符串时总是 returns false

String.equals-method always returns false while comparing two Strings

我大学的任务是编写摩尔斯电码解码器。我有字符串数组,其中包含 "morse-alphabet" 的每个字母。在 for 循环中,我使用 substring-method 将 morsecode 中的句子切成 "morseletters"。我制作了另一个 for 循环和一个 if 语句,以检查 "morse-alphabet" 中的哪个字母与当前 "morseletter" 匹配。我使用了 String.equals 方法,但它不起作用。即使两个字符串相同。我还打印了循环中每个字符串的长度,以检查字符串是否包含一些不需要的空格。但是即使字符串看起来相同并且长度相同,我的 if 语句的条件也永远不会为真。问题似乎出在 equals 方法上。我必须更改什么才能让我的代码正常工作?

public class Morse {
private static String[] morsecodes = { ".-", "-...", "-.-.", "-..", ".",
         "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
         "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
         "-..-", "-.--", "--.." }; 

public static void  main (String args[]) {
    System.out.println(decodeMorseCode(".... . .-.. .-.. --- .-- --- .-. .-.. -.."));       
}
public static String decodeMorseCode(String morseText) {
    String realText = "";
    String morseLetter = "";
    int counter = 0;

    for(int i = 0; i < morseText.length(); i++) {   
        if((morseText.charAt(i)==' ')) {
            morseLetter = morseText.substring(counter,i);
            counter = i+1;
        }
        if(morseText.charAt(i)==' '||i+1==morseText.length()) {
            for (int j = 0; j < 26; j++) {
                if((morsecodes[j].equals(morseLetter))) { //this is the if-statemen which causes the problem
                    char c = (char)(j+97);
                    realText += c;
                }

                if(j+1<=morseText.length()) {
                    if(morseText.charAt(j)==' '&& morseText.charAt(j+1)==' ') {
                        realText += " ";
                    }
                }

            morseLetter = "";
            }
        }           
    }
    return realText;
}

}

删除行 morseLetter = ""; 如下所示,您的代码将起作用。

    if(morseText.charAt(i)==' '||i+1==morseText.length()) {
        for (int j = 0; j < 26; j++) {
            if(morsecodes[j].equals(morseLetter)) { //this is the if-statemen which causes the problem
                char c = (char)(j+97);
                realText += c;
            }

            if(j+1<=morseText.length()) {
                if(morseText.charAt(j)==' '&& morseText.charAt(j+1)==' ') {
                    realText += " ";
                }
            }
            //morseLetter = "";
        }
    }           

已经有一些答案,但也许你会想考虑这种方法:

public static String decodeMorseCode(String morseText) {
    String realText = "";
    String morseLetter = "";
    int counter = 0;
    List<String> morseMessage;

    //Easier splitting of the String with morse code
    String[] morseLetters = morseText.split(" ");
    morseMessage = Arrays.asList(morseLetters);

    for (String morse : morseMessage) {
        for (String letter : morsecodes) {
            if (morse.equals(letter)) {
                System.out.println(letter);
                //Here comes mapping from Morse-to-English.
            }
        }
    }
    return realText;
}

我在这里所做的是引入一个列表,它可以逐个字母地存储您的消息,并使循环更容易。这是通过首先将字符串拆分为包含单个字母的数组,然后将其转换为列表来完成的。然后,我将摩尔斯电码消息中的每个字母与您的摩尔斯电码进行比较 "dictionary",如果您 运行 这样做,您会发现它能够识别每个字母。

剩下的就是创建一个地图,将摩尔斯电码表示形式翻译成英文字母。 注意:也许最好不要摩尔斯电码数组,而是将其作为带有英文映射的地图?

例如:

private static Map<Character, String> morseToEnglish;

public static void  main (String[] args) {
    morseToEnglish = new HashMap<Character, String>();
    morseToEnglish.put('a', ".-");
    morseToEnglish.put('b', "-...");
    morseToEnglish.put('c',  "-.-");
    morseToEnglish.put('d',  "-..");
    ...

然后循环映射:

    for (String morse : morseMessage) {
        for (String letter : morseToEnglish.values()) {
            if (morse.equals(letter)) {
                for (Character character : morseToEnglish.keySet()) {
                    if (morseToEnglish.get(character).equals(letter)) {
                        System.out.print(character);
                    }
                }
            }
        }
    }

您用莫尔斯编码的消息是 'helloworld'