5 Replies - 277 Views - Last Post: 05 April 2013 - 07:33 AM Rate Topic: -----

#1 Jack_Ruby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-March 13

Error in finding a palindrome

Posted 04 April 2013 - 09:13 PM

I'm working on a program that searches through a text file and then determines if it is a palindrome or not. This is the code I have so far, but I think there are errors. Any help is appreciated!

*A palindrome is a word or sequence of words that has the same sequence of letters when read either forwards or backwards. EX: "Ma is as selfless as I am"

public boolean isRegularPalindrome( String s ) 
	{
s = s.replaceAll("\\W", "").toLowerCase(); // remove space and punctuation
		
		int length = s.length();
		System.out.println(s);
		System.out.println(length);
		if( length == 1 || length == 0)
		{
			return true;
		}
		else {
			if( s.charAt(0) == s.charAt(length - 1))
				isRegularPalindrome(s.substring(1, length - 1));
		}
		return false;
	}


	public boolean isRegularPalindrome( String s ) 
	{
		s = s.replaceAll("\\W", "").toLowerCase(); // remove space and punctuation
		
		int length = s.length();
		System.out.println(s);
		System.out.println(length);
		if( length == 1 || length == 0)
		{
			return true;
		}
		else {
			if( s.charAt(0) == s.charAt(length - 1))
				isRegularPalindrome(s.substring(1, length - 1));
		}
		return false;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Error in finding a palindrome

#2 Jack_Ruby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-March 13

Re: Error in finding a palindrome

Posted 04 April 2013 - 09:21 PM

Apologize for posting the code twice. The second one is easier to read.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,088
  • Joined: 27-December 08

Re: Error in finding a palindrome

Posted 04 April 2013 - 10:42 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Error in finding a palindrome

Posted 05 April 2013 - 07:20 AM

it is your if/else that are not correctly implemented and actually you don't need the else

		if( length == 1 || length == 0)
			return true;
		
		if( s.charAt(0) == s.charAt(length - 1))
			return isRegularPalindrome(s.substring(1, length - 1));
		
		return false;


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,901
  • Joined: 20-September 08

Re: Error in finding a palindrome

Posted 05 April 2013 - 07:24 AM

You're meant to use recursion? If so, remove the replaceAll call as it's needlessly inefficient - call it before the recursive method
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7947
  • View blog
  • Posts: 13,532
  • Joined: 19-March 11

Re: Error in finding a palindrome

Posted 05 April 2013 - 07:33 AM

	public boolean isRegularPalindrome( String s ) 

		else {
			if( s.charAt(0) == s.charAt(length - 1))
				isRegularPalindrome(s.substring(1, length - 1));  // 
		}



You need a return statement here.

	public boolean isRegularPalindrome( String s ) 

		else {
			if( s.charAt(0) == s.charAt(length - 1))
				return isRegularPalindrome(s.substring(1, length - 1));  // 
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1