For some reason the true and false are considered "incompatible types". Am I only suppose to run this through a boolean method? What's wrong with it.
for(int i = 0; i < array.length ; i++)
{
int val = (array[i] % 2);
if(val == 0)
array[i] = true;
else
array[i] = false;
}
Well array
is probably an int[]
, given that you're using array[i] % 2
and assigning the result to an int
.
There's no conversion from boolean
to int
, so you can't store your result back in the int[]
array. It's not clear what you're trying to do, but that's why it's not compiling.
Aside
If you had a separate boolean[]
of the same size, that would work - although it would be more simply written as:
boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
even[i] = (array[i] % 2) == 0;
}
Any time you find yourself with:
if (someCondition) {
doSomething(true);
} else {
doSomething(false);
}
you should consider refactoring it to:
doSomething(someCondition);
See more on this question at Stackoverflow