• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning int types to a boolean expression

 
Michael Mikhail
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I`m new to java and i`m tring to understand something. i have the following program



and i need to make the program show 1 and 0 instead of true and false. How can i do that? from what i read i can`t assign an integer value to a boolean expression. i just can`t figure it out. Thanks!
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an if statement. If the value that you're checking is true, print out "1" else print out "0".
 
Michael Mikhail
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking on that but i was wondering if i have to create an if statement for each case or to create a general if statement to solve all 4 situations.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to check each result (e.g. whether p&q is true) individually, so you'll need multiple if statements. If you want to do the value-checking and printing in a single line for each case, you can also have a look at the ternary operator.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You should use the shortcut operators && and || rather than & and | for booleans, unless you specifically need to evaluate the right operand as well as the left.
Have you come across the ?: operator? And the % tags? The ?: operator will allow you to get 1 and 0 out of true and false, and the % tags are a nice way to print themNote you should use %n rather than \n in this context, but \t is all right. You can use a similar technique to get the output
1 && 0: 0     1 || 0: 1
... and you can reuse an argument with a number and a $ sign after the %.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You should use the shortcut operators && and || rather than & and | for booleans, unless you specifically need to evaluate the right operand as well as the left.
Have you come across the ?: operator? And the % tags? The ?: operator will allow you to get 1 and 0 out of true and false, and the % tags are a nice way to print themNote you should use %n rather than \n in this context, but \t is all right. You can use a similar technique to get the output
1 && 0: 0     1 || 0: 1
... and you can reuse an argument with a number and a $ sign after the %.

I completely forgot about printf()!

@OP; follow Campbell's suggestion. It's much cleaner than having a bunch of if statements.
 
Rumesh Eranga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using ternary operator is way easy than having so many if conditions

Regards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic