boolean values into a string

Discussion in 'Java' started by NAJH, Jul 23, 2006.

  1. NAJH

    NAJH Guest

    I'm a newbie to Java so be patient with my simplistic questions... :eek:)

    I have a boolean result from something and I want to put it into a
    property value, against a given key. Not sure how to do this though.

    // background on why I want to do this:
    The boolean result is from using a roots[].canWrite() for a particular
    file. I want to populate a property object with a load of these
    canWrite() results corresponding to different root directories.

    Many thanks!
    NAJH, Jul 23, 2006
    #1
    1. Advertising

  2. NAJH

    NAJH Guest

    NAJH wrote:
    > I'm a newbie to Java so be patient with my simplistic questions... :eek:)
    >
    > I have a boolean result from something and I want to put it into a
    > property value, against a given key. Not sure how to do this though.
    >


    I've ended up using an if statement to check whether the boolean is
    true or false and then writing a temporary string with the value "true"
    or "false", which is then used in the Properties class

    I feel there must be a better way though.
    NAJH, Jul 23, 2006
    #2
    1. Advertising

  3. NAJH

    Lee Weiner Guest

    In article <>, "NAJH" <> wrote:
    >
    >NAJH wrote:
    >> I'm a newbie to Java so be patient with my simplistic questions... :eek:)
    >>
    >> I have a boolean result from something and I want to put it into a
    >> property value, against a given key. Not sure how to do this though.
    >>

    >
    >I've ended up using an if statement to check whether the boolean is
    >true or false and then writing a temporary string with the value "true"
    >or "false", which is then used in the Properties class
    >
    >I feel there must be a better way though.


    There are other ways. "Better" is subjective, however.

    String str = Boolean.toString( boolenvalue);

    Lee Weiner
    lee AT leeweiner DOT org
    Lee Weiner, Jul 23, 2006
    #3
  4. NAJH

    jtl.zheng Guest

    one more way:
    public static String String.valueOf(boolean b)
    jtl.zheng, Jul 23, 2006
    #4
  5. NAJH wrote:
    > I'm a newbie to Java so be patient with my simplistic questions... :eek:)
    >
    > I have a boolean result from something and I want to put it into a
    > property value, against a given key. Not sure how to do this though.
    >
    > // background on why I want to do this:
    > The boolean result is from using a roots[].canWrite() for a particular
    > file. I want to populate a property object with a load of these
    > canWrite() results corresponding to different root directories.
    >
    > Many thanks!
    >


    I agree with the previously posted responses pointing out Java API
    methods to do what you want. Also, java.io.PrintStream.printf() has a %b
    format specifier.

    However, I have a more general comment about your questions. I get the
    impression that you have programmed previously, but not in Java, and you
    are trying to program by applying techniques from other languages.

    In some cases, you can tell there must be a better way, and ask about it
    here. There will be other cases in which there is a better way, but the
    way you are using works just well enough to keep you from asking about it.

    If you have not already done so, I strongly recommend getting a book on
    Java and reading it in parallel with practicing coding in Java.

    Patricia
    Patricia Shanahan, Jul 23, 2006
    #5
  6. NAJH

    NAJH Guest

    Patricia Shanahan wrote:
    <snip>
    > However, I have a more general comment about your questions. I get the
    > impression that you have programmed previously, but not in Java, and you
    > are trying to program by applying techniques from other languages.
    >
    > In some cases, you can tell there must be a better way, and ask about it
    > here. There will be other cases in which there is a better way, but the
    > way you are using works just well enough to keep you from asking about it.
    >
    > If you have not already done so, I strongly recommend getting a book on
    > Java and reading it in parallel with practicing coding in Java.
    >
    > Patricia


    Thanks for all your comments everyone.

    Hi Patricia,
    Yep, you're quite right. I've 'programmed' in other languages. (In
    quotes because I don't think I'm terribly good.) I've dabbled with
    BASIC, VB in *all* its variations (even the Australian variety),
    JavaScript, touched on C and C++ at uni, done a little ActionScript
    (that's the stuff Adobe Flash uses), loads of SQL, even some UML and
    OCL (why does OCL exist?)... I've not really got firmly to grips with
    an OO language though.

    To be honest I'm doing a course in Java in my spare time and so have
    several books of various sorts. But sometimes it's a little difficult
    to get into the real grime of what's going on without playing around
    with stuff. Examples in books are fine, but they don't engage the brain
    much do they?

    The nicest reference book I have so far is one of the O'Reilly ones.
    Java in a Nutshell I think. I grabbed it from the library. Do you have
    any recommendations? I'm not really after a "course" type book but a
    damn good reference I guess...

    All the best,
    Neil
    NAJH, Jul 24, 2006
    #6
  7. NAJH wrote:
    > Patricia Shanahan wrote:
    > <snip>
    >> However, I have a more general comment about your questions. I get the
    >> impression that you have programmed previously, but not in Java, and you
    >> are trying to program by applying techniques from other languages.
    >>
    >> In some cases, you can tell there must be a better way, and ask about it
    >> here. There will be other cases in which there is a better way, but the
    >> way you are using works just well enough to keep you from asking about it.
    >>
    >> If you have not already done so, I strongly recommend getting a book on
    >> Java and reading it in parallel with practicing coding in Java.
    >>
    >> Patricia

    >
    > Thanks for all your comments everyone.
    >
    > Hi Patricia,
    > Yep, you're quite right. I've 'programmed' in other languages. (In
    > quotes because I don't think I'm terribly good.) I've dabbled with
    > BASIC, VB in *all* its variations (even the Australian variety),
    > JavaScript, touched on C and C++ at uni, done a little ActionScript
    > (that's the stuff Adobe Flash uses), loads of SQL, even some UML and
    > OCL (why does OCL exist?)... I've not really got firmly to grips with
    > an OO language though.
    >
    > To be honest I'm doing a course in Java in my spare time and so have
    > several books of various sorts. But sometimes it's a little difficult
    > to get into the real grime of what's going on without playing around
    > with stuff. Examples in books are fine, but they don't engage the brain
    > much do they?
    >
    > The nicest reference book I have so far is one of the O'Reilly ones.
    > Java in a Nutshell I think. I grabbed it from the library. Do you have
    > any recommendations? I'm not really after a "course" type book but a
    > damn good reference I guess...


    As long as you are doing a course in Java you should be OK. That will
    make sure you learn about arrays, collections etc.

    For an API reference, I strongly recommend
    http://java.sun.com/j2se/1.5.0/docs/api/index.html. I keep a downloaded
    copy on each computer where I do Java development.

    Generally, the best way I've found to learn programming languages is to
    alternate reading with attempts at programming in the language, which is
    what you seem to be doing.

    Good luck,

    Patricia
    Patricia Shanahan, Jul 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vighnesh
    Replies:
    3
    Views:
    1,026
    Paul Clement
    Aug 4, 2005
  2. J Leonard
    Replies:
    4
    Views:
    12,663
    Mark Space
    Jan 19, 2008
  3. krishna
    Replies:
    2
    Views:
    435
    samwyse
    Jun 25, 2009
  4. vighnesh
    Replies:
    3
    Views:
    181
    Paul Clement
    Aug 4, 2005
  5. Metre Meter
    Replies:
    7
    Views:
    363
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page