Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

assigning int types to a boolean expression

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75642
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using ternary operator is way easy than having so many if conditions

Regards.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic