views:

359

answers:

4

I had a question regarding a basic program I was writing said whether a word 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) ) ); 

}
+4  A: 

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.

Brandon Bodnár
Hi bodnarbm! Get off my turf.` ``:P`
jjnguy
Am I missing something with that code? It looks exactly like the one in the question except for the superfluous "String d = ..." line. Or is it just there for completeness for your other points?
paxdiablo
There for completeness. You are correct that I could have express myself better. In that the code he posted before should be correct, and that he should look elsewhere for where the problem lies.
Brandon Bodnár
Okay, that makes sense. And I agree you're probably right about where the fault lies. Given the amount of use String.equals() has got, it should be bug-free by now :-)
paxdiablo
+5  A: 

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.

paxdiablo
A: 

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

Jonathan Mayhak
-1 for telling him how to solve his homework and doing so poorly.StringBuilder is preferred over StringBuffer
Tim Bender
I didn't write any code. I just mentioned possible ways of solving the problem. Does it really matter if stringbuilder is preferred over stringbuffer? It's just a simple problem and he needs to experiment.
Jonathan Mayhak
+1. @JM is right, I think. He didn't post the code which is what we all frown upon for homework. The hints are relatively obvious but still require a fair bit of work by the OP. And this level of homework could be forgiven for choosing the less efficient way (I code Java every day and still think my knowledge of the entire library is in the sub-25% range). It's certainly more efficient than rolling your *own* reverseString().
paxdiablo
if it's a homework, they probably need to create basic methods without usage of advanced objects like StringBuilder.
eugeneK
A: 

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.

True Soft