943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 38351
  • Java RSS
Feb 13th, 2008
-1

Converting boolean to integer

Expand Post »
Is there an easy way to convert a simple integer into a boolean type?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
orcboyx is offline Offline
18 posts
since Dec 2007
Feb 13th, 2008
0

Re: Converting boolean to integer

Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Feb 13th, 2008
0

Re: Converting boolean to integer

(i != 0)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,758 posts
since May 2007
Feb 14th, 2008
0

Re: Converting boolean to integer

use:
java Syntax (Toggle Plain Text)
  1. boolean b;
  2. int iSomeValue;
  3.  
  4. b=true;
  5. if(iSomeValue==0)b=false;
or use function
java Syntax (Toggle Plain Text)
  1. boolean intToBool(int iVal)
  2. {
  3. boolean b=true;
  4. if(iVal==0)b=false;
  5. return b;
  6. }
Last edited by DangerDev; Feb 14th, 2008 at 3:07 am. Reason: uncomlete
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Feb 14th, 2008
0

Re: Converting boolean to integer

That is a lot more code than is needed. The conversion only needs the evaluation of intVal!=0, so the function reduces to
Java Syntax (Toggle Plain Text)
  1. boolean intToBool(int value){
  2. return (value != 0);
  3. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,758 posts
since May 2007
Feb 16th, 2008
0

Re: Converting boolean to integer

Thanks for the help, by th way i used ezzral's method it was quick and simple, i also found out i could've just started with a boolean by setting random generator class to boolean type.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
orcboyx is offline Offline
18 posts
since Dec 2007
Jul 6th, 2010
0
Re: Converting boolean to integer
Use

Java Syntax (Toggle Plain Text)
  1. int intVal = (boolVal) ? 1 : 0;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Member 784921 is offline Offline
1 posts
since Jul 2010
Jul 6th, 2010
0
Re: Converting boolean to integer
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.
Reputation Points: 925
Solved Threads: 504
Posting Expert
NormR1 is offline Offline
5,086 posts
since Jun 2010
Jul 7th, 2010
0
Re: Converting boolean to integer
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)

Java Syntax (Toggle Plain Text)
  1. class BoolInt {
  2. public static final int INT_TRUE = 1;
  3. public static final int INT_FALSE = 0;
  4.  
  5. public BoolInt() {
  6. }
  7.  
  8. private int i = INT_FALSE;
  9.  
  10. public int getIntValue() {
  11. return i;
  12. }
  13.  
  14. public boolean getBoolValue() {
  15. return i==INT_TRUE;
  16. }
  17.  
  18. public void setIntValue(int i) throws Exception {
  19. // if the user doesn't enter 1 or 0 throw exception
  20. if (i!=INT_TRUE || i!=INT_FALSE) throw new Exception("your message");
  21.  
  22. this.i=i;
  23. }
  24.  
  25. public void setBoolValue(boolean b) {
  26. i = (b)?INT_TRUE:INT_FALSE
  27. }
  28. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: question on ambiguity in java
Next Thread in Java Forum Timeline: Java Developer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC