开发者

Checking to see if a string is a palindrome with String.equals()

I had a question regarding a basic program I was writing said whether a wor开发者_JAVA百科d such as racecar is a palindrome or not.

All my methods which reverse the string, strip the punctuation work but the one that determines if it is a palindrome does not.

/**
* Determines if a series of letters makes a palinedrome
* 
* @param  str   All punctuation and spaces have been removed 
*               before this method is called.  
* @return true  if phrase is a palindrome,
*         false otherwise.
*/
public boolean isPalindrome(String str)
{
   String d = reverseString (str); 
   return( str.equals (reverseString (str) ) ); 

}


Okay, I'm not sure what purpose d is meant to serve in your function since it's never used but, if you want to see why your function's not working, just add debug code:

public boolean isPalindrome (String str) {
    System.out.println ("DEBUG: original string = '" + str + "'");
    System.out.println ("DEBUG: reverse string = '" + reverseString (str) + "'");
    if (str.equals (reverseString (str)))
        System.out.println ("DEBUG: returning true");
    else
        System.out.println ("DEBUG: returning false");
    return str.equals (reverseString (str));
}

I'd bet money on there being something wrong with your reverseString function (but not much money). These debug statements should give you enough to figure out where the problem lies.


If string reverseString(String string), and all whitespace was removed then checking if something is a palindrome should be

public boolean isPalindrome(String string)
{
    return string.equals(reverseString(string));
}

Granted this is case sensitive so if your palindrome definition does not care about casing, then use equalsIgnoreCase instead.

If this does not work, then you may want to check your stripping and reverseString methods again.


Your problem is the reverse string method you have not shown. If that method is working properly then your isPalindrome method should work. All you need to do is fix your reverse string method.

Java does not have a native reverse string method, and I highly recommend you write your own.

Java does, however, have a reverse method for StringBuffer and StringBuilder. StringBuilder is preferred over StringBuffer.

Use the equals method to compare your reversed string to the original string


The code should be like this:

String d = reverseString (str); 
return( str.equals (d) ); 

You don't have to call twice reverseString()

P.S.: StringBuffer has a method that reverses a String.


I'm sure you've already submitted your homework by now, but I'm learning java and needed the practice, so here's my code for you. It uses a char array and reverses that. I'd assume the best way would be to use a StringBuilder, but the intention of your homework is probably to learn to do it yourself:

public class reverseString {
    public static void main(String[] args) {
        System.out.println("racecar is a palindrome: "+ isPalindrome("racecar"));
    }

    public static boolean isPalindrome(String str)
    {
       String d = reverseString (str); 
       return( str.equals (reverseString (str) ) ); 
    }

    private static char[] reverse(char[] input) {
        int length = input.length;
        char[] reversed = new char[length];
        for (int i=0;i<length;i++) {
            reversed[length-i-1]=input[i];
        }
        return reversed;
    }

    private static String reverseString(String input){
        String reversed = new String(reverse(input.toCharArray())); 
        return reversed;
    }   
}

Output:

racecar is a palindrome: true

If anyone has any comments on why my code sucks, fire away. I'd appreciate any constructive criticism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜