problem with recursive boolean method
Hello, I wrote this method to return true if the String is a palindrome, false if not. But my method always returns false. I know this is because I have a return false at the end of the method but it will not compile without a return value. Any suggestions?
publicstaticboolean isPal(String s, String temp,int count)
{
if(s.length() == count)
{
if (s.equalsIgnoreCase(temp))
{
returntrue;
}
else
{
returnfalse;
}
}
else
{
temp = temp.concat(s.substring(s.length() - count));
s = s;
count = count +1;
isPal(s, temp, count);
}
returnfalse;
}
Thanks!
[1428 byte] By [
Matt_Ha] at [2007-11-26 19:22:27]

Try this. Not tested and no way guarnteed to work.return isPal(s, temp, count);This goes in the else part and get rid of the return statement at the end.
Try taking out the else
{
return false;
}
and why do you bother saying s = s;
Message was edited by:
TheGuy@YourWindow
The s=s; was just put there out of frustration hoping it would do something. I'm still getting the same problem with these methods.
> just put there out of frustration hoping it would do something. That's the single worst method of software development I can imagine. It's worse than doing nothing.~
I never said I was Bill Gates.
I think your problem is that substring is zero indexed, while count is not, so:
temp = temp.concat(s.substring(s.length() - count));
should be:
temp = temp.concat(s.substring(s.length() - count - 1));
(I think).
Sorry, while length() isn't, not count.
Message was edited by:
abu5e
by definition a palindrome is a sequence of chars that has the property of reading the same in either direction
example: "asdfdsa"
so, why are you comparing two strings?
I would go like this
public static boolean isPal(String str) {
return str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());
}
Why is he comparing two strings? Because a String is a palindrome if "asd" equals the reverse of "dsa" and you can ignore the centre letter if the length is an odd number. However it appears OP's algorithm is incorrect.
Yeah but what he is saying is that, rather than comparing two strings, you could compare one string to itself (like he showed)
(Although technically, your suggestion is comparing two strings because of the toString() and strings being immutable, still a far better solution though)
Message was edited by:
abu5e
That is still comparing two strings though!P.S. Although that maybe a neater solution, this is most likely a homework assignment to write a recusive method.Message was edited by: flounder
> Why is he comparing two strings? Because a String is
> a palindrome if "asd" equals the reverse of "dsa" and
> you can ignore the centre letter if the length is an
> odd number. However it appears OP's algorithm is
> incorrect.
My question was incorrect, it should have been "why are you making the method take more parameters than it needs to accomplish the task"
my english has some limits =( (Chilean guy here...)
> I would go like this...
I'm betting that recursion is a requirement, e.g.:public static boolean isPalindrome(String s) {
if (s == null) return false;
int len = s.length();
if (len < 2) return true;
if (s.charAt(0) == s.charAt(len - 1)) {
return isPalindrome(s.substring(1, len - 1));
}
return false;
}
~
Thanks everyone for the help. I ended up using:return isPal(s, temp, count);I found the problem was with my substring, it is now:temp = temp.concat(s.substring(s.length() - count -1, s.length()-count));Thanks again!
I wrote a similar non-recursive method for an applet previously:
if(e.getSource() == enter)
{
StringBuilder b = new StringBuilder(word.getText());
b.reverse();
if(b.toString().equalsIgnoreCase(word.getText()))
{
messageDisplay.setText("The word " + word.getText() + " is a palindrome.");
}
else
{
messageDisplay.setText("The word " + word.getText() + " is not a palindrome.");
}
}
But yes recursion was a requirement this time.