Java 比较两个字符串时,String.equals-method始终返回false

Java 比较两个字符串时,String.equals-method始终返回false,java,arrays,string,equals,morse-code,Java,Arrays,String,Equals,Morse Code,我大学的任务是给莫尔斯电码解码器编码。我有一个字符串数组,里面有“摩尔斯字母表”的每个字母。在for循环中,我使用substring方法将morsecode中的句子剪切成“morseletters”。我已经做了另一个for循环和if语句,以检查“morse字母表”中的哪个字母与当前的“morseletter”匹配。我用过String.equals-method,但不管用。即使两个字符串相同。我还打印了循环中每个字符串的长度,以检查字符串是否包含一些不需要的空格。但即使字符串看起来一样,长度也一样

我大学的任务是给莫尔斯电码解码器编码。我有一个字符串数组,里面有“摩尔斯字母表”的每个字母。在for循环中,我使用substring方法将morsecode中的句子剪切成“morseletters”。我已经做了另一个for循环和if语句,以检查“morse字母表”中的哪个字母与当前的“morseletter”匹配。我用过String.equals-method,但不管用。即使两个字符串相同。我还打印了循环中每个字符串的长度,以检查字符串是否包含一些不需要的空格。但即使字符串看起来一样,长度也一样,if语句的条件也永远不会为真。问题似乎是等号法。为了让我的代码正常工作,我必须改变什么

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;
}
公共级莫尔斯电码{
私有静态字符串[]摩尔斯编码={“-”,“-…”,“-.-”,“-…,”,
"..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--.." }; 
公共静态void main(字符串参数[]){
System.out.println(解码莫尔斯电码(“…-…-…-…-…-”);
}
公共静态字符串解码MORSECODE(字符串morseText){
字符串realText=“”;
字符串morseLetter=“”;
int计数器=0;
对于(inti=0;i如果(j+1删除行
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 = "";
        }
    }           
if(morseText.charAt(i)=''|| i+1==morseText.length()){
对于(int j=0;j<26;j++){
if(morsecodes[j].equals(morseLetter)){//这是导致问题的if语句
字符c=(字符)(j+97);
realText+=c;
}

如果(J+1<P>)已经有了一些答案,但也许你会想考虑这个方法:

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;
}

你用莫尔斯编码的信息是
'helloworld'

你调试过看
莫尔斯莱特
是什么吗?当我打印出来时,莫尔斯莱特的值是有意义的。它们总是包含一个莫尔斯字母,没有任何不需要的空格。如果我是你,我会打印出
莫尔斯莱特
,然后直接打印在
mo之后rsecodes[j]
,然后如果它们看起来相同,则打印每个
字符串的
hashCode()
,谢谢,它没有修复我的代码,但这可能对将来的作业非常有帮助。字符串数组是作业的一部分。但非常感谢您的解决方案。
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);
                    }
                }
            }
        }
    }