Reputation: 23
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
Reputation: 56471
)
for the getPalindrome
method.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++)
.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