1

I have to compare Boolean values in a array by using the isPalindrom() method. I cant not get my program to accurately return the answer if an array is or is not a palindrome. It just always returns true instead of false when I purpose use a non palindrome answer. Code:

 public Boolean isPalindrome()
 {
    Boolean result = true;
    for(int i=0;i<bits.length;i++)
    {
        Boolean a = bits[i];
        Boolean b = bits[bits.length - i - 1];
        if(a!=b)
            result = false;      
    }
    return result;    
 }
3
  • 1
    For starters, why are you using Boolean? Commented Jan 28, 2016 at 3:26
  • For homework, it evidently makes it easier to compare the array as a palindrome. Commented Jan 28, 2016 at 3:33
  • Fairly soon you'll want to learn the difference between the wrapper types (Boolean) and the primitives (boolean). Unless you're putting it into a collection, there's almost never a reason to use the wrapper for boolean. Commented Jan 28, 2016 at 3:38

4 Answers 4

1

Try the code below:

public class TestC {

    public static void main(String[] args) {
        Boolean[] bits = { Boolean.FALSE, Boolean.TRUE, Boolean.FALSE,
                Boolean.FALSE };
        Boolean[] bits1 = { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE,
                Boolean.TRUE };
        Boolean[] bits2 = { Boolean.FALSE, Boolean.TRUE, Boolean.FALSE };
        System.out.println(isPalindrome(bits));
        System.out.println(isPalindrome(bits1));
        System.out.println(isPalindrome(bits2));

    }

    public static Boolean isPalindrome(Boolean[] bits) {
        Boolean result = true;
        for (int i = 0; i < bits.length; i++) {
            Boolean a = bits[i];
            Boolean b = bits[bits.length - i - 1];
            if (a != b)
                result = false;
        }
        return result;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cant use a static method in the Boolean isPalindrome method, I am having to create a constructor, in a separate class, to use in the main class. The static method just throws serval mismatches.
1

Perhaps using Integer type (if values are all numeric) would work better than boolean for (a) and (b)?

public Boolean isPalindrome()
     {
        Boolean result = true;
        for(int i=0;i<bits.length;i++)
        {
            int a = bits[i];
            int b = bits[bits.length - i - 1];
            if(a!=b)
                return false;      
        }
        return true;    
     }

6 Comments

Still didn't fix my issue, I know it is just in the lines of codes that I posted. Because when I change the return value to false in the if statement, it interprets the array to not be a palindrome.
Rather than store the true/false in a variable, why not simply return false if a mismatch is found and then return true at the end ... I've updated my solution accordingly.
That still did not fix the issue. I have tried all different types of comparison values, even use the compareTo and ect. It just won't return correctly if it is or is not a palindrome.
The only item I might try is to remove the boolean type for a and b. I'm not certain what type of values "bits" is comparing, but a standard var type might work better. Otherwise, it would almost certainly return true every time. I've updated my answer again ... we'll get it.
Can't change the boolean to an integer, wont let me do that
|
1

instead of != use below code

if (!(a.Equals(b)))

Hope it works.

Comments

0

The method looks correct, even though it's not the optimized/efficient one. It should return the correct true/false for the palindrome test. Could you provide the set of inputs you're testing with?

2 Comments

I am just using one array that I passed the Boolean variables into through the constructor from the main class, its called bits.
Your code is working for a boolean array e.g. Boolean[] array. Could you show some more code how you're defining and passing the array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.