• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting a String to Boolean

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you post the exact error message you get.

Layne
 
Jamie Wool
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ShrubListParser.java:194: incompatable types
found java.lang.Boolean
required: boolean
active = Boolean.valueOf((String)abdata);

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Since:
1.5



oh-oh , ofcourse !! a bit stupid of me, I should have checked that. Anyways, one lives and learns.

Thanks,
ram.
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic