| Author |
Converting a String to Boolean
|
Jamie Wool
Ranch Hand
Joined: May 03, 2004
Posts: 49
|
|
I'm having problems doing the above, have tried numerous things but everything errors, which is frustrating when it's quite a simple thing i want to accomplish(breathe).
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
It would help if you post the exact error message you get. Layne
|
Java API Documentation
The Java Tutorial
|
 |
Jamie Wool
Ranch Hand
Joined: May 03, 2004
Posts: 49
|
|
ShrubListParser.java:194: incompatable types found java.lang.Boolean required: boolean active = Boolean.valueOf((String)abdata);
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
You are trying to assign a Boolean object to a boolean primative. That won't work. You need to call a method on your Boolean object to get the primative value from it before you can assign it to a primative.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
ramprasad madathil
Ranch Hand
Joined: Jan 24, 2005
Posts: 489
|
|
>active = Boolean.valueOf((String)abdata); As Paul said above, this returns a Boolean object. You will have to convert it to primitive boolean However this has the effect of first creating a Boolean object from your String object and then converting it inot primitive boolean. Isnt that too many objects for such a task. Always optimize where you can. Consider using this The above directly converts the String object to boolean primitive without the overhead of an intermediate Boolean object. ram.
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
Originally posted by ramprasad madathil: The above directly converts the String object to boolean primitive without the overhead of an intermediate Boolean object.
The method you want is parseBoolean(String), though getBoolean(String) is related:
public boolean getBoolean(String name) Returns true if and only if the system property named by the argument exists and is equal to the string "true".
|
 |
ramprasad madathil
Ranch Hand
Joined: Jan 24, 2005
Posts: 489
|
|
Originally posted by David Harkness: The method you want is parseBoolean(String), though getBoolean(String) is related:
David, Yes, thanks, teaches me to read the api a bit more carefully here on without taking things for granted, though the method name is a bit misleading, in my humble opinion. Curiously the method I searched for was parseBoolean() to begin with (I used javap for the search), couldnt find it and that's when I saw this getBoolean() method. Dint bother to check up javadoc to see what it does. And when I read your post, I was wundering how could I have missed the parseBoolean() method? That's when I went to javadoc and found that there's indeed no parseBoolean() method either Am I missing soemthing here? Looks like, unlike other primitives, converting a String to boolean requires converting it to a boolean object first. ram.
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
Originally posted by ramprasad madathil: Yes, thanks, teaches me to read the api a bit more carefully here on without taking things for granted, though the method name is a bit misleading, in my humble opinion.
I've started looking up methods when posting answers here, usually to quote something, but also to make sure I'm not thinking of the wrong method. And I agree about the name. I don't see why Boolean.getProperty() wasn't clear enough. Clearly a get method on the Boolean class should need to say it's getting a Boolean!
That's when I went to javadoc and found that there's indeed no parseBoolean() method either Am I missing soemthing here?
No, I missed something:
public static boolean parseBoolean(String s) ... Since: 1.5
Oops!
|
 |
ramprasad madathil
Ranch Hand
Joined: Jan 24, 2005
Posts: 489
|
|
Since: 1.5
oh-oh , ofcourse !! a bit stupid of me, I should have checked that. Anyways, one lives and learns. Thanks, ram.
|
 |
 |
|
|
subject: Converting a String to Boolean
|
|
|