boolean values into a string

N

NAJH

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!
 
N

NAJH

NAJH said:
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.
 
L

Lee Weiner

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
 
P

Patricia Shanahan

NAJH said:
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
 
N

NAJH

Patricia Shanahan wrote:
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
 
P

Patricia Shanahan

NAJH said:
Patricia Shanahan wrote:


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top