简体   繁体   English

String.equals-method 在比较两个字符串时总是返回 false

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

My task for university my was to code a Morse-Code-Decoder.我在大学的任务是编写一个莫尔斯电码解码器。 I have String-Array with every letter of the "morse-alphabet" in it.我有字符串数组,其中包含“莫尔斯字母表”的每个字母。 In a for-loop I cut the sentence in morsecode into it's "morseletters" using the substring-method.在 for 循环中,我使用 substring-method 将莫尔斯码中的句子切成“莫尔斯莱特”。 I've made another for-loop and and an if-statement, to check which letter from the "morse-alphabet" matches the current "morseletter".我做了另一个 for 循环和一个 if 语句,以检查“莫尔斯字母表”中的哪个字母与当前的“莫尔斯莱特”相匹配。 I've used the String.equals-method, but it does'nt work.我使用了 String.equals 方法,但它不起作用。 Even if the two Strings are the same.即使两个字符串相同。 I also printed the length of every String in the loop, to check if the Strings contained some unwanted spaces.我还打印了循环中每个字符串的长度,以检查字符串是否包含一些不需要的空格。 But even though the Strings looked the same and had the same length, the condtion of my if-statement would never be true.但即使字符串看起来相同且长度相同,我的 if 语句的条件也永远不会为真。 The problem appers to be the equals-method.问题似乎是equals-method。 What do I have to change, so that my code works?我必须更改什么才能使我的代码正常工作?

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

} }

Remove the line morseLetter = "";删除行morseLetter = ""; as shown below, and your code will work.如下所示,您的代码将起作用。

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

There's some answers already, but maybe you would want to consider this approach:已经有一些答案,但也许您想考虑这种方法:

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

What I've done here is introduce an List that would store your message letter-by-letter and that makes it easier to loop through.我在这里所做的是引入一个列表,它将一个字母一个字母地存储您的消息,这使得循环更容易。 This is done by first splitting the String into an Array containing individual letters, and then converting it into a List.这是通过首先将字符串拆分为包含单个字母的数组,然后将其转换为列表来完成的。 I then compare each letter from the Morse code message to your Morse code "dictionary", and if you run this you will see that it is able to recognize each individual letter.然后我将摩尔斯电码消息中的每个字母与您的摩尔斯电码“字典”进行比较,如果您运行它,您将看到它能够识别每个单独的字母。

What is left is to create a Map to translate Morse code representation into English-alphabet letters.剩下的就是创建一个 Map 来将莫尔斯电码表示翻译成英文字母。 Note : maybe it would be better to not have an array of Morse codes, but have it as a Map with English mappings?注意:也许没有莫尔斯电码数组会更好,而是将其作为带有英文映射的地图?

For example:例如:

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',  "-..");
    ...

And then loop and map:然后循环并映射:

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

The message you morse-encoded was 'helloworld' .你莫尔斯编码的消息是'helloworld'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM