• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive Methods  RSS feed

 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to write a program to check for palindromes and I have the palindrome checker running but I am failing to write a recursive method to eliminate spaces and punctuation. I've tried several times and keep getting errors. Here is my code so far:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch! When posting code, please UseCodeTags(←click) so that your code will be more readable. I've added them for you this time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that the method to remove spaces and punctuation has to be recursive? If it's a separate method, to pre-process the string before testing, then I wouldn't expect it to be recursive.

On the other hand, if you're just going to recursively check the raw string as-is for palindromity, and ignore the spaces and punctuation inline, then inside your recursive "check if it's a palindrome" method, you just need to skip over these characters by not making another recursive call when you encounter them.

So, if it's a separate method, then you can--and should!--get the "strip spaces" and "check palindrome" methods working completely independently of each other. Neither one has to know anything about the other. Write, test, and debug them in isolation.

If it's a single recursive method that skips spaces inline, then get the method working first without that consideration. Just don't worry about spaces and punctuation at first. Feed it only strings with none of those characters, and get it working that way. Then move on to handling spaces.

And in either case, if you get stuck and need to ask for help, be sure to TellTheDetails(←click) about exactly what problems you're having so that people will be able to help you.

Happy coding!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't see the point of making a recursive method for that task, since you can accomplish goal with exactly one line of code, using replaceAll(String regex, String replacement) method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!