2

I'm practicing making my own String reversal tool. to top the learning process of I used the method in a simple application. BUT..
When I run my code in Eclipse, Even if word is = racecar and reversed becomes = racecar (word reversed). the else is displayed.. Telling me that word and reverse are never equal.. but..ion that case they are? Right? Please give me some advice, this is quite the bummer. Thank you.

import java.util.Scanner;

public class Main {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args){

    System.out.println("Please enter a word, I'll show it reversed ");
    System.out.println("and tell you if it is read the same forward and backward(A Palindrome)!: ");
        String word = sc.nextLine();
        String reverse = reverse(word);

        if (reverse == word){
            System.out.println("Your word: " + word + " backwards is " + reverse + ".");
            System.out.println("Your word is read the same forward and backward!");
            System.out.println("Its a Palindrome!");
        }

        else {
            System.out.println("Your word: " + word + " backwards is " + reverse + ".");
            System.out.println("Your word is not read the same forward and backward!");
            System.out.println("Its not a Palindrome!");
        }



    }

    public static String reverse(String source){

        if (source == null || source.isEmpty()){
            return source;
        }

        String reverse = "";

        for(int i = source.length() -1; i >= 0; i--){
            reverse = reverse + source.charAt(i);
        }

        return reverse;
    }

}
0

4 Answers 4

10

Always use String#equals to compare Strings. == will compare if the references are equal which doesn't really work for comparing equal strings due to how they are stored.

if (reverse.equals(word))
Sign up to request clarification or add additional context in comments.

2 Comments

Wow. Thank you so much! No kidding I knew that.. It just slipped my mind. :O
@AndrewFabian you should accept the answer if you feel it solves your problem.
3

The reason as to why you are not getting the if to show up, ever is becuase you cannot use == for strings. You can only do that in math/int/double/float etc comparison

For String comparison, you must use

 equals(String s) 

or for words that don't have to be exactly the same Capitalization

 equalsIgnoreCase(String s)

2 Comments

Ummm... those methods don't exist. Use a.equals(b) or a.equalsIgnoreCase(b).
thats what I meant to put, got confused with something else, thanks for the correction!
3

you should use the equals method to compare the two strings,the string is a object,this is a reference,the equals() method will compare the content of two strings,but the == will compare the address of two strings

so,you should write like this:

if (reverse.equals(word))

Comments

1

In Java, objects (including Strings) are stored as references--their locations in memory.

The == operator checks to see if these references are equal, not if the actual strings are equal.

Fortunately, Java has a workaround. String method equals(String) will return true if both Strings have identical contents. Use this for Strings, not ==.

1 Comment

Thank you all for the help! I knew about String.equals(string) but i used it for things like menus. So i used if ("yes".equals(input)){ I feel dumb.

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.