E.Scott
E.Scott

Reputation: 23

Trying to flip an array around but keep getting an error

Attempting to test and see if an array is a palindrome, however, the flipArray method I created keeps giving me trouble. The compiler gives a "not a statement" error and I'm not sure what is stopping it. The code is supposed to flip array b around, then compare array a and array b to see if they are the same:

public class Lab13_2{
    public static final int SIZE = 50;
    public static void main (String [] args){
        Boolean palindrome = false;
        String[] a = {"hello" , "goodbye", "goodbye" , "hello"};
        String[] b = new String[SIZE];


        b = a.clone();     
        palindrome = getPalindrome(a,b,a.length);
    }

    public static boolean getPalindrome(String[] a, String[] b, int arrayLength{
        b = flipArray(b);
        for(int i = 0; i <arrayLength; i++){
            if(a[i] != b[i]){
                return false;
            }
        }
        return true;
    }

    public static String[] flipArray(String[] array){
        for(int=0; i <array.length/2; i++){
            int temp = array[i];
            array[i] = array[array.length-1-i];
            array[array.length-1-i] = temp;
        }
        return array;    
        }
    }

Upvotes: 0

Views: 48

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56471

  1. you're missing the closing ) for the getPalindrome method.
  2. your for loop within the flipArray method doesn't declare i before it's used within the condition or elsewhere. should be for(int i = 0; i < array.length/2; i++).
  3. your temp variable should be of type String rather than int.

Lastly but not least you don't compare strings like this:

if(a[i] != b[i])

change it to this:

if(!a[i].equals(b[i]))

Upvotes: 1

Related Questions