File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes assigning int types to a boolean expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assigning int types to a boolean expression" Watch "assigning int types to a boolean expression" New topic
Author

assigning int types to a boolean expression

Michael Mikhail
Greenhorn

Joined: Oct 17, 2011
Posts: 5
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

Joined: Apr 23, 2009
Posts: 157

Use an if statement. If the value that you're checking is true, print out "1" else print out "0".


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Michael Mikhail
Greenhorn

Joined: Oct 17, 2011
Posts: 5
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

Joined: Apr 23, 2009
Posts: 157

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

Joined: Oct 13, 2005
Posts: 38808
    
  23
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

Joined: Apr 23, 2009
Posts: 157

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

Joined: Jan 26, 2011
Posts: 34
Using ternary operator is way easy than having so many if conditions

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assigning int types to a boolean expression