• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bad operand types for binary operator?

 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am working on a program to make a program in which the user plays craps. I know there are more problems, as this is a work in progress, but first I need to complete public static boolean play before I can move forward. When I try to compile, I get an error message that says "bad operand types forbinary '||' first type: int; second type: boolean" while the compiler highlights "|| result ==11" what does this error message mean and can someone point me in the right direction of how to fix it? Or is it a simple fix that I am missing? Any help is GREATLY appreciated. I have banging my head on the wall over this one for a couple hours now.


Once again, I cannot give enough thanks for any help.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Brian Mart,

Welcome to CodeRanch!

So, below is the problematic line:

Here, you can easily see what is wrong here.

Hint : operator '||' requires both operands to be boolean. What is your first operand?

I hope this helps.
 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anayonkar, but aren't both of my operands "result"? I am new to Java so I am not entirely sure. If my operands are "result" why does the first one on that same line work just fine and the error message only appears on the second instance of result? Also, according to my instructions, result needs to be an int. So confused...
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

Here's another hint : what's the difference between '==' and '='?

P.S. I'm giving you hints because it would be nice if you yourself understood what is wrong - instead of getting a ready-made answer. I hope you don't mind
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are comparing Different things if result =7|| result==4, One side of || contains an integer and the other side a boolean. Both sides need to be either....Got it now?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote: . . . Both sides need to be either....Got it now?
No, both sides must be “boolean”. It will work with a small b or a capital B because of un-boxing (since Java5 in 2004). The details are in the Java Language Specification, but they are slightly different for the | operator, which can take ints as operands.

Anyway, as has already been mentioned, the real error was confusing == and =.
 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it was just a simple oversight but the compiler message just confused me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic