# Thread: in need of help (easy)

1. Member Join Date
Dec 2009
Posts
3
Rep Power
0

## in need of help (easy)

The problem is easy to solve, i cant figure it out myself tho...:D

i got a table with boolean, it got two vars p and q (p = false q = true)

I want to make the true and the false 0 and 1 instead of p and q

p and q is the var for the true false.

Anyways, i know it has something to do with casting, conversion or the if to get.

the conversion should be boolean to int right? kinda lost...

Help is greatly appreciated  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## You can't cast: boolean values don't have any intrinsic numerical value or vice versa.

"conversion" is an extremely vague term. Certainly you want to start with false and end up with zero, so you could say the false value is changed or converted. But this just begs the question: how?

You are left with using an if statement. Try that. If you get stuck, post your code and describe what goes wrong (compiler error or output that differs from what you want etc).

If you have studied the ternary ?:, it does something similar to if.  Reply With Quote

3. Member Join Date
Dec 2009
Posts
3
Rep Power
0

## Heres the code

As you see on the first line i have tried with a if statement but i dont know how to connect it with the f = 0

Java Code:
```// Project 2-2: a truth table for the logical operators.
class LogicalOpTable {
public static void main(String args[]) {

boolean p, q;
int t, f;

t = 1; f = 0;

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

[B]p = true; q = false;[/B]
[B]if(p==true); f=0; [/B]
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));
}
}```
Last edited by fasck; 12-30-2009 at 08:21 PM.  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## if(p==true); f=0;

Close, but using an if statement it would be

Java Code:
```if(p) {
f = 0;
} else {
f = 1;
}```

Notice how if(p) is the same as if(p==true). And how the value of f has to be set for either option. (by the way, didn't you say you wanted zero, not one, for false?)

One way of achieving what you are trying to do would be to use a separate method to do the conversion.

Java Code:
```public class BooleanDisplay {
public static void main(String[] args) {
boolean p, q;

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

p = true;
q = false;
System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));

p = false;
q = false;
System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));
}

static int asInt(boolean b) {
if(b) {
return 1;
} else {
return 0;
}
// same thing
//return b ? 1 : 0;
}

// not used, but it could be!
static void display(boolean p, boolean q, boolean r) {
System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(r));
}
}```  Reply With Quote

5. Member Join Date
Dec 2009
Posts
3
Rep Power
0

## Thanks alot mate =)  Reply With Quote

6. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## You're welcome.  Reply With Quote

#### Posting Permissions

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