• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive Java Methods  RSS feed

 
Jacqueline Turof
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm suppose to be creating two recursive methods. One to figure out fibonaccis numbers and one to figure out if a string is a palindrome. I keep getting the error that my palindrome method must return a boolean. However, I feel like I have done that and don't understand why I'm getting this error message. I just want to test my methods to make sure the logic is correct, but can't even get that far. Any help is appreciated, thanks!



 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see in the palindrome method whether you are returning boolean in all the if-else conditions? You are missing the return in one of the if's
 
Jacqueline Turof
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thanks I see it now. Sometimes you look at something so many times you stop actually seeing what's there. lol Thanks, again!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacqueline Turof wrote:Ah, thanks I see it now. Sometimes you look at something so many times you stop actually seeing what's there. lol Thanks, again!

A few more things for you:
Have you been told that your recursive method must be the only method? Because recursive solutions often have an outer non-recursive method that deals with pre- and post-processing.

Your isPalindrome() method is a classic case in point. By not having an "outer" method, you're forced to call toLowerCase() on each invocation, whereas you could just hand off an "already coverted" String to an internal recursive method.

Also, your if(Character.isLetter(first)&& Character.isLetter(last)) test is a wrinkle that is likely to make your method fail when it's not supposed to. I understand what you're trying to do, but the fact is that a String like:
Madam, I'm Adam
is NOT palindromic unless you first remove all non-letter characters...or you make your first and last logic a lot smarter.

Winston
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have been told about that particular Fibonacci implementation as an example of poor performance?
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the original example does not even compile due to some minor glitches.
Anyway this code portion is obviously wrong:

Hints.
What is the use of assigning a new value to the variable palindrome is not using it?
What should be returned instead of true?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!