# Thread: Beginners Problem Boolean to Int?

1. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Beginners Problem Boolean to Int?

Hi there, I recently bought Java: A Beginners Guide in an attempt to learn my first programming language, I completed the first chapter no problems but a question in chapter 2 has me stumped.
Try This 2-2, Display a Truth Table: Create a program that displays the truth table for Javas Logical Operators. I did this by following the example in the book:

Java Code:
```
class LogicalOpTable {
public static void main(String[]args) {

boolean p, q;

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

p = true;
q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = true; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = false; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = false; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

}
}```
Thats grand, but the last part of the question "modify the program so that it displays 1's and 0's rather than true or false" is where I run into problems.

I have tried lots of different ways to do this none being successful. The one that makes most sense to me is trying to cast incompatible types, which I have tried but the complier says they are inconvertible types.

Java Code:
```class LogicalOpTable {
public static void main(String[]args) {

boolean p, q;
int i;

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

p = true;
q = true;
i = (int)( p ); System.out.print(i); //Complier says inconvertible types.
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = true; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = false; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

p = false; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));

}
}```
I can't find anything in the previous two chapters to help me solve this. it actually says in chapter 2 boolean and int are not compatible. The author of this book is mocking me! So I need someone to give me a hint and point me in the right direction. Is it possible to solve this problem by casting incompatible types or are my barking up the wrong tree? Should I be trying if statements instead? Something like if(true)System.out.print(1). I just want a hint, not a complete answer. Thanks for taking the time to help me!
Last edited by ShiftyBiscuits; 08-11-2013 at 04:25 PM.

2. ## Re: Beginners Problem Boolean to Int?

Originally Posted by ShiftyBiscuits
Something like if(true)System.out.print(1).
You're very very close; have you learned how to use else in conjunction with if?

db

3. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Re: Beginners Problem Boolean to Int?

Originally Posted by DarrylBurke
You're very very close; have you learned how to use else in conjunction with if?

db
Thank you for your quick reply, I know about if{}else{} but that isn't covered until Chapter 3 so I thought this must not be the solution the author is looking for since this question is asked in chapter 2. Is it possible to do it with the if statement alone? The if statement was covered in chapter 1 so this might be what the author is looking for?
Last edited by ShiftyBiscuits; 08-11-2013 at 05:10 PM.

4. ## Re: Beginners Problem Boolean to Int?

Well, that if-else can also be written as two if statements ...

db

5. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Re: Beginners Problem Boolean to Int?

Ah I see, I will give the if statement approach another go. Thanks for your help.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•