Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
That is a lot more code than is needed. The conversion only needs the evaluation of intVal!=0, so the function reduces to
boolean intToBool(int value){
return (value != 0);
}
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Why do you want to convert a boolean to an int.
It makes no sense. A boolean has 2 values, an int has billions.
Why would one of the billion values be true and another be false?
0 and NOT 0 are C concepts. Nothing in java corresponds.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
This thread has been solved but I would also like to add a small suggestion. If for some reason you want to use ints for boolean expressions, why don't you make a class for it. Call it: BoolInt. Have it have one int private attribute and with the help of public set methods make sure that the only valid values are 1 and 0 (or whatever you want)
class BoolInt {
public static final int INT_TRUE = 1;
public static final int INT_FALSE = 0;
public BoolInt() {
}
private int i = INT_FALSE;
public int getIntValue() {
return i;
}
public boolean getBoolValue() {
return i==INT_TRUE;
}
public void setIntValue(int i) throws Exception {
// if the user doesn't enter 1 or 0 throw exception
if (i!=INT_TRUE || i!=INT_FALSE) throw new Exception("your message");
this.i=i;
}
public void setBoolValue(boolean b) {
i = (b)?INT_TRUE:INT_FALSE
}
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448