user1514362
user1514362

Reputation: 127

incompatible data types: boolean and int

I get this error when I try to compile.

imcomparible types: boolean and int

result= (result) && (found_list[i] !=0);

Why do I get this error? How do I fix it??

Upvotes: 0

Views: 573

Answers (4)

Galaxin
Galaxin

Reputation: 484

Boolean takes only True of False as values and nothing else like other datatypes such as int,long,double,short etc...

Hence if result is a boolean the value you store in it should also be a boolean that is either True or False.

result= (result) && (found_list[i] !=0); 

Here (result) is either True of False.If you have just intialized it and left like

Boolean result;

Then the value here is false.Else if you have modified it somewhere before this it would be the respective value.

Now (found_list[i] !=0); is a wrong way of coding. If found_list[] is a boolean, you cannot compare it with a int value(0 in this case).

What you or trying to do here is true/false!=0 which can not be compared.

Hence you are getting this error.You can instead change it to

result= ((result) && (found_list[i]))

Upvotes: 0

Prasanth
Prasanth

Reputation: 5258

Unlike languages c, c++ and many other, 0 & 1 aren't false & true in java.

This is a common mistake. Also, doing checking for something like boolean != false is the exact same as checking if boolean is true. i.e.

if(b != false)

is same as

if(b)

So, you can do

result = result && found_list[i];

Upvotes: 2

Sujay
Sujay

Reputation: 6783

Since found_list[] is a boolean array, you can't compare with an int value. (found_list[i] !=0) is wrong.

The way to do it would be:

result= (result) && (found_list[i]);

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166396

You are trying to compare found_list[i] of type boolean to 0 of type int

Then change

result= (result) && (found_list[i] !=0); 

to

result= (result) && (found_list[i]); 

Upvotes: 2

Related Questions