0

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?

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
    }
}
1
  • just a comment here, why don't you do a toLower() on the "decide" String (and compare it against ("morse") so you don't have to tell users to "Pay attention to Caps". Just a thought! Commented Aug 22, 2012 at 23:12

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

Using charAt to correct for character comparison:

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:

english[0] = '1';

Comments

0

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

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.