String值和数组值之间的相等性

时间:2021-05-17 22:51:55

So this is a portion of my code. If i entered " 1111 " for the preTranslation String, it should print out true but it prints out false. I'm guessing that .equal() cannot compare the two values but I don't know another way. Any help?

所以这是我代码的一部分。如果我为preTranslation字符串输入“1111”,它应打印出true,但打印出false。我猜测.equal()无法比较这两个值,但我不知道另一种方式。有帮助吗?

import javax.swing.JOptionPane;
import java.util.Arrays;

public class test3
{
    public static void main ( String [] args )
    {

    String s1 = "Morse";

    // Decide whether Morse code or English
    String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation. Pay attention to Caps.");

    // Enter String & decide whether to convert to Morse or English
    String preTranslation = JOptionPane.showInputDialog("Enter the words you wish to translate.");

    if ( decide.equals( s1 ))
        toMorse( preTranslation );

    else
        toEnglish( preTranslation );
    }

// Translate to Morse
public static void toMorse( String preTrans )
{
    // Initialize english letters
    char[] english = new char[3];


    // Initialize english numbers
    english[0] = 1;
    english[1] = 2;
    english[2] = 3;

    // Initialize morse characters
    String[] morse = {".","-", ".-"};

    // Replace spaces with |
    String phraseWithDelimiter = preTrans.replace( " ", "|");

    String[] translation = new String[phraseWithDelimiter.length()];
    System.out.println( phraseWithDelimiter.substring(0, 1).equals( english[0] )); // Should print out to be true
    }
}

4 个解决方案

#1


2  

You are comparing a String object with a character (a char) so equals will always return false. If you want to get a single character from a string, use charAt method instead of substring.

您正在将String对象与字符(char)进行比较,因此equals将始终返回false。如果要从字符串中获取单个字符,请使用charAt方法而不是substring。

#2


0  

Use String.valueOf(phraseWithDelimiter.substring(0, 1)).equals( english[0] ));

使用String.valueOf(phraseWithDelimiter.substring(0,1))。equals(english [0]));

#3


0  

Using charAt to correct for character comparison:

使用charAt校正字符比较:

phraseWithDelimiter.charAt(0) == english[0]

You define :

你定义:

english[0] = 1;

which is ASCII 1, a character which cannot be entered by the user. You need to define:

这是ASCII 1,用户无法输入的字符。你需要定义:

english[0] = '1';

#4


0  

Just to elaborate on JamesB's answer, if you would look at the implementation of the equals method of the String class:

只是详细说明JamesB的答案,如果你看一下String类的equals方法的实现:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String) anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                        return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

You would notice that if the object you're comparing to is not an instance of a String (which in your example is a character), it immediately returns false. :)

您会注意到,如果您要比较的对象不是String的实例(在您的示例中是字符),则会立即返回false。 :)

#1


2  

You are comparing a String object with a character (a char) so equals will always return false. If you want to get a single character from a string, use charAt method instead of substring.

您正在将String对象与字符(char)进行比较,因此equals将始终返回false。如果要从字符串中获取单个字符,请使用charAt方法而不是substring。

#2


0  

Use String.valueOf(phraseWithDelimiter.substring(0, 1)).equals( english[0] ));

使用String.valueOf(phraseWithDelimiter.substring(0,1))。equals(english [0]));

#3


0  

Using charAt to correct for character comparison:

使用charAt校正字符比较:

phraseWithDelimiter.charAt(0) == english[0]

You define :

你定义:

english[0] = 1;

which is ASCII 1, a character which cannot be entered by the user. You need to define:

这是ASCII 1,用户无法输入的字符。你需要定义:

english[0] = '1';

#4


0  

Just to elaborate on JamesB's answer, if you would look at the implementation of the equals method of the String class:

只是详细说明JamesB的答案,如果你看一下String类的equals方法的实现:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String) anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                        return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

You would notice that if the object you're comparing to is not an instance of a String (which in your example is a character), it immediately returns false. :)

您会注意到,如果您要比较的对象不是String的实例(在您的示例中是字符),则会立即返回false。 :)