Results 1 to 11 of 11
  1. #1
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Question use boolean as 0 or 1

    Hi,

    I just started programming with Java, and I came from TI-basic (the language texas instruments' graphing calculators can be programmed on), and there is one thing I'd really like to know.

    on TI-basic, if I wanted to use a boolean (like a=5) to change another, variable like b, so say I would like to make b 8 if a is 5, and 6 if a isnt 5. I would just type:
    b = 6+2(a=5)
    since a=5 outputs 1 if it's true and 0 if it isn't.

    In java, I would have to type
    Java Code:
    if (a == 5)
    {
    b = 8;
    }
    else
    {
    b=6;
    }
    So my question is, is there any easy way to use booleans in equations like in TI-basic, or just any other way that needs less command lines?

  2. #2
    Boysie is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

    Need Help!!

  3. #3
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Boysie View Post
    My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

    Need Help!!
    sorry Aaron, please create your own topic instead of replying to my topic that has virtually nothing to do with your question.

    If you send me an e-mail, I might be able to help you since I created a system like that a year ago. e-mail: Joost.meulenbeld[at]gmail[dot]com

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by joost_m View Post
    So my question is, is there any easy way to use booleans in equations like in TI-basic, or just any other way that needs less command lines?
    Suprised to see the topic in the Applet thread. Anyway...

    you can use easy boolean statement like a

    Java Code:
    int b=(a==5)?8:6;
    instead of

    Java Code:
    if (a == 5)
    {
    b = 8;
    }
    else
    {
    b=6;
    }

    Good Java Coding
    Last edited by Webuser; 04-12-2010 at 03:12 PM.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Boysie View Post
    My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

    Need Help!!
    Welcome to forum. :)

    Please don't post unrelated question next time in threads. It could mess-up lots of members. If your question is not related with the original please start a new thread, in relevant sub-forum. Good luck.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Webuser View Post
    Suprised to see the topic in the Applet thread.
    I'll move the thread to correct place.

    @OP, please choose the correct sub-forum next time when you posting again.

  7. #7
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry, I'll post in the right sub-forum next time.
    to webuser: thanks for the post, this really helps.
    The solution is useful in this case. However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints. Thanks, topic can be closed

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by joost_m View Post
    However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints.
    You mean the multiple nested if conditions?

    Quote Originally Posted by joost_m View Post
    Thanks, topic can be closed
    It's better to mark thread solved by yourself. Since you are the person looking the solution, and satisfied on it.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by joost_m View Post
    Sorry, I'll post in the right sub-forum next time.
    to webuser: thanks for the post, this really helps.
    The solution is useful in this case. However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints. Thanks, topic can be closed
    IMHO it was one of the stupidest mistakes to omit that 'feature' from C just because of an explicit boolean type, but that's just me; I love the fiddling with zero and non-zero values being implicitly considered false and true respectively. It allows for very terse code and booleans don't even exist as such in the JVM, it's all 0s and 1s anyway.

    Nice question b.t.w. I happen to own a TI92+, a TI Voyage and an HP50G and none of my TIs can handle those tricks but they have a Motorola MC68000 on board so I can program them in C ;-) That HP50G is an entirely different story.

    kind regards,

    Jos

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Terse code is not all the descriptive, though.
    Treating a numeric as a boolean never made sense to me, so the dropping of it from Java was no problem. This isn't coding to the metal, after all...and trying to code like that in Java doesn't make much sense to me.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Terse code is not all the descriptive, though.
    Treating a numeric as a boolean never made sense to me, so the dropping of it from Java was no problem. This isn't coding to the metal, after all...and trying to code like that in Java doesn't make much sense to me.
    Having programmed in C for more than thirty years leaves its marks, I know. Coincidentally a colleague and I had a (friendly) little discussion today about how to flip the value of a boolean variable; I did it like this:

    Java Code:
    boolean b= ...;
    b^= true; // flip
    he objected that many younger programmers might not understand this and coined this:

    Java Code:
    boolean b= ...;
    b= !b; // flip
    Look at the compiled code (javap) and see the generated byte code for the two variants.

    kind regards,

    Jos

Similar Threads

  1. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:32 PM
  2. Boolean value not working?
    By zerkz in forum New To Java
    Replies: 3
    Last Post: 09-29-2009, 06:42 AM
  3. Boolean problems
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 09-15-2009, 10:57 PM
  4. transfer boolean to 1's and 0's
    By Nikohw in forum Java Applets
    Replies: 5
    Last Post: 09-12-2009, 09:05 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 AM

Tags for this Thread

Posting Permissions

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