Question

public static boolean palindrome(String input, int i, int j)
 {
  if (i >= j)
   return true;
  if (input.charAt(i) == input.charAt(j))
  {
   i++;
   j--;
   palindrome(input, i, j);
  }
  else if (input.charAt(i) != input.charAt(j))
   return false;
 }

My Java platform (eclipse) won't accept this code as working, due to a "lack of return type." Now I know in proper coding ediquite, it's better to use only one return value, but when it comes to recursion, this is somewhat new to me. How can I go about doing this? If I instantiate a Boolean type at the top of this method, it's creating a new instance of that variable (and instantiating it as null or whatever I set it to) each time the method runs, but if I place it above my constructor, my method won't assign a value to it/can't return it.

Basically, how do I go about modifying my code to have a single return value that Eclipse will accept as always executing? I can do this easily enough with loops, but I'm not sure how to approach the topic with Recursion.

Was it helpful?

Solution

You can certainly just do this:

return palindrome(input, i, j);

However it is good practice to have a single return to improve readability. Try this on for size:

   boolean isPalindrome = false; 
   if (i >= j)
   isPalindrome = true;
   else if (input.charAt(i) == input.charAt(j))
  {
   i++;
   j--;
   isPalindrome = palindrome(input, i, j);
  }
  else if (input.charAt(i) != input.charAt(j))
   isPalindrome = false;
  return isPalindrome;
 }

Have that boolean always instantiated. The key here is to make palindrome's return be stored in that boolean.

The recursive portion comes in at the call to palindrome. It will only finally return the final value after all of the recursive calls, because it only ever determines if its a palindrome when it reaches the end of the recursive cycle.

OTHER TIPS

Just for improved readability:

public static boolean palindrome(String input, int i, int j)
{
    if (i >= j)
        return true;

    if (input.charAt(i) != input.charAt(j))
        return false;

    return palindrome(input, i + 1, j - 1);
}

If we could use C# and Linq (since I don't have Java dev environment here) to reversing a char array:

public static bool palindrome(String input)
{
    return input.Equals(new String(input.ToCharArray().Reverse().ToArray()));
}

The issue is that you have no return inside the second if statement. If charAt i and j are equal you never return anything.

Keeping the spirit of your code I'd shorten the whole thing up to be:

public static boolean palindrome(String input, int i, int j)
{
  if (i >= j)
  {
    return true;
  }

  if (input.charAt(i) == input.charAt(j))
  {
    return palindrome(input, i + 1, j - 1);
  }

  return false;
}

In the second if block, you don't return anything.

Just change the recursive call so that you return its value:

  return palindrome(input, i, j);

Also, incidentally, you don't need to do i++ and j-- in the if block-- you can instead call the palindrome method with i+1 and j-1 instead, and that will have basically the same effect.

        public static String palindrome(String input, int i, int j)
         {

            if (i >= j)
                return "-1";    

         //--------------------------------------------//   

          if (input.charAt(i) == input.charAt(j))
          {
              i++;
              j--;

         //--------------------------------------------//         

              palindrome(input, i, j);
              return "is palindrom";      
          }

         //--------------------------------------------//     

          else 
              return "not palindrom";
          }
           }
         //--------------------------------------------//

An other option might be this:

boolean isPalindrome(String s) {

        boolean ret = true;

        if (s.length() == 1 || s.equals("")) {
            return ret;
        } else {
            char first = s.charAt(0);
            char last = s.charAt(s.length() - 1);
            if (first != last)
                ret = false;
            else if (s.length() > 2) {
                String partial = s.substring(1, s.length() - 1);
                ret = isPalindrome(partial);
            }
        }
        return ret;
    }

Probably this answer can help(in Python):

def isPalinr(str1):
    if len(str1)<=1:
        print('Palindrome')
        return
    if (str1[0] != str1[-1]):
        print ('Not palindrome')
        return
    else:
        return isPalinr(str1[1:len(str1)-1])

This will return None though we can return string in first 'if' clause because program will ultimately stop at that point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top