Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   boolean values into a string (http://www.velocityreviews.com/forums/t359968-boolean-values-into-a-string.html)

NAJH 07-23-2006 02:43 PM

boolean values into a string
 
I'm a newbie to Java so be patient with my simplistic questions... :o)

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 07-23-2006 03:32 PM

Re: boolean values into a string
 

NAJH wrote:
> I'm a newbie to Java so be patient with my simplistic questions... :o)
>
> 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.


Lee Weiner 07-23-2006 03:58 PM

Re: boolean values into a string
 
In article <1153668721.383689.208990@m73g2000cwd.googlegroups .com>, "NAJH" <neilonusenet@yahoo.co.uk> wrote:
>
>NAJH wrote:
>> I'm a newbie to Java so be patient with my simplistic questions... :o)
>>
>> 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

jtl.zheng 07-23-2006 04:30 PM

Re: boolean values into a string
 
one more way:
public static String String.valueOf(boolean b)


Patricia Shanahan 07-23-2006 04:45 PM

Re: boolean values into a string
 
NAJH wrote:
> I'm a newbie to Java so be patient with my simplistic questions... :o)
>
> 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

NAJH 07-23-2006 11:05 PM

Re: boolean values into a string
 

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


Patricia Shanahan 07-24-2006 12:44 AM

Re: boolean values into a string
 
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


All times are GMT. The time now is 12:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.