Hello, I am making a program that tries to identify if an inputted word is a palindrome( word or number that reads backwards and forwards the same) but with the Pali() method of my program I have found out that when I compare the substring of letters it always ends up to be false and print out test2. I really don't see why if some one inputed "racecar" the r from the left and the r from the r from the right are any different. Anyways here is my code and I hope you guys can help me figure out what I did wrong or am not including. :)

/**
 * Palinidrome
 * 
 * @author 
 * @version 1/18/11
 */
import java.util.Scanner;
public class Palindrome
{ 
    String[] letter;
    int b=0;
    boolean pal = true;
    Palindrome(String w){
        letter=new String[w.length()];
        for(int a =0; a<w.length();a++){
            letter[a] = w.substring(a,a+1);
        }
    }
    public boolean Pali(){
        System.out.println(letter[b] +"=="+ letter[b+letter.length-1]);
        if(letter[b] == letter[b+letter.length-1]){
            pal =true;
            System.out.println("test1");
            Pali();
        }
        else if (letter[b] != letter[b+letter.length-1]){
            pal = false;
            System.out.println("test2");
        }
        return pal;
    }
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a word");
        Palindrome p = new Palindrome(s.next());
        boolean yay =p.Pali();
        if(yay==true){
            System.out.println("It is a palindrome");
        }
        else if(yay==false){
            System.out.println("It is not a palindrome");
        }
    }
}

You're using == to compare Strings. Bad idea. When used with non-primitive types (like Strings), == checks to see whether the two values are references to the exact same object, not whether the objects they refer to are considered equal. "r" and "r" may be equal, but they could be two different objects, in which case a == comparison would return false.

You could fix this by replacing all instances of string1 == string2 with string1.equals(string2), but there's a better way. Instead of declaring a String[] and filling it with one-character substrings, declare a char[] and use the String.charAt method to fill it. (Instead of w.substring(a, a + 1) , write w.charAt(a) .) Char is a primitive type, so == works fine for char comparisons.

You're using == to compare Strings. Bad idea. When used with non-primitive types (like Strings), == checks to see whether the two values are references to the exact same object, not whether the objects they refer to are considered equal. "r" and "r" may be equal, but they could be two different objects, in which case a == comparison would return false.

:) thanks for the reply. This cleared everything up for me.

Great. Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.