• Post Reply Bookmark Topic Watch Topic
  • New Topic

incompatible types: int cant be converted to boolean  RSS feed

 
antonio arango
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I just started with java and im stuck already :-(

i am using netbeans 8.0 - using the following code, one line works the other dosent - WHY???
something about "incompatiable types - int cant be converted to boolean"
which i would be ok if i got the same syntax error on the other line also, but i dont and the only
difference is the equal to and not equal to.

i cant figure it what is the syntax error? to me they are both interpreting a condition. any explanation ?

public static void main(String[] args) {
int i = 0;
int zro = 0;

-- this line does not work; get syntax error
if ( i=0 && (i % zro) == 0) System.out.println("not good");

-- this line works; no syntax error
if (i!=0 && (i % zro) == 0) System.out.println("good");
}
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.


You got the syntax error because the line


uses the assignment = operator in if ( i=0 &&....)

You ought to be using == operator which is equals.
 
antonio arango
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG !!! like DAA !!! you are so right and i never noticed it LOL - thank you
Ok so why the so missleading syntax error. why not the "you fool thats not how you do an equal comparison" !
thats what really stomped me i guess. Thanks again :-)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
antonio arango wrote:Ok so why the so missleading syntax error. why not the "you fool thats not how you do an equal comparison" !


How is the compiler to know? It is acceptable to have an assignment there, and sometimes that is what you WANT. I believe there are tools that will warn you if you do something like this, but it is not a syntax error.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not very often though. I'm not a fan of using an assignment as an expression.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Not very often though. I'm not a fan of using an assignment as an expression.

I agree with you here 100%. I would avoid it like the plague for exactly what Antonio just went thought.

But it IS legal according to the syntax of the language, so the compiler has no way to detect it.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: spell your variables correctly. Use zero rather than zro. Then you will be able to see that
i % zro
is equivalent to
i % zero
and that is equivalent to division by zero, so you can expect a nice Exception if you ever get that code to compile and run. You will have to think again why you are trying to get the remainder after division by zero.

[edit]Change “divison by zero” to a hyperlink[/edit]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!