87

sorry to be a pain... I have: HashMap<String, String> o

o.get('uses_votes'); // "1"

Yet...

Boolean.parseBoolean(o.get('uses_votes')); // "false"

I'm guessing that ....parseBoolean doesn't accept the standard 0 = false 1 = true?

Am I doing something wrong or will I have to wrap my code in:

boolean uses_votes = false;
if(o.get('uses_votes').equals("1")) {
    uses_votes = true;
}

Thanks

0

11 Answers 11

101

It accepts only a string value of "true" to represent boolean true. Best what you can do is

boolean uses_votes = "1".equals(o.get("uses_votes"));

Or if the Map actually represents an "entitiy", I think a Javabean is way much better. Or if it represents configuration settings, you may want to take a look into Apache Commons Configuration.

9
  • 2
    ah thats a bit rubbish! ok, its good it wasn't just me. I'll use your suggestion. :) Thanks Oct 12, 2011 at 1:06
  • 1
    Ideally parseBoolean would through an exception if it would get something other than true or false.
    – Gabe
    Oct 12, 2011 at 1:08
  • or it could be a little bit more lenient! But yeah, I agree - it was difficult to find the problem. Oct 12, 2011 at 1:10
  • 3
    @Gabe on the contrary, Boolean.parseBoolean(java.lang.String) does not throw Exception if it gets something other than true or false. It just returns false everytime it gets something other than "true" or "TRUE" as string. grepcode
    – WarFox
    Sep 24, 2013 at 7:18
  • 1
    @WarFox: Yes, that's why I said ideally it would throw an exception. If it threw an exception on input it didn't understand, there would never be any question of whether it parsed properly or just defaulted to false.
    – Gabe
    Sep 24, 2013 at 7:22
30

I have a small utility function to convert all possible values into Boolean.

private boolean convertToBoolean(String value) {
    boolean returnValue = false;
    if ("1".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || 
        "true".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value))
        returnValue = true;
    return returnValue;
}
3
  • 15
    Why not just return "1".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value);
    – Tom Fraser
    Aug 4, 2016 at 8:16
  • Compare how Ansible does it: docs.ansible.com/ansible/YAMLSyntax.html#truthiness Oct 18, 2016 at 8:22
  • Your on clause seems weird. If the possible values are on and off then this makes sense, but on isn't a definitive true/false situation like yes/no or 1/0 is. What if my possible values are on and under for example, then this logic wouldn't work. However, I usually try to set my booleans on DB side and simply use POJO to convert Database 1 and 0 into Java Boolean. Jan 31, 2018 at 3:13
14

According to the documentation (emphasis mine):

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

0
10

If you're trying to get C's behavior (0 == false and everything else is true), you could do this:

boolean uses_votes = Integer.parseInt(o.get("uses_votes")) != 0;
1
  • Ah, thats an interesting work around, yes I guess that is the behaviour I was expecting. In this case I can guarantee that the output will either be 1 or 0 so BalusC's code works perfectly, however I shall remember this as it seems very useful. Oct 12, 2011 at 1:16
4

I know this is an old thread, but what about borrowing from C syntax:

(o.get('uses_votes')).equals("1") ? true : false;
2
  • 14
    The ? true : false bit at the end is a well-known antipattern: it's just 14 extra characters typed for no particular reason, as the left-hand-side is already a boolean, and testing it to decide whether to return true or false is silly.
    – amalloy
    Aug 29, 2013 at 3:53
  • Thanks its help me Mar 27, 2018 at 15:22
3

Thomas, I think your wrapper code, or just the condition itself, is the cleanest way to do what you want to do in java, which is convert "1" to the Boolean True value. Actually, comparing to "0" and taking the inverse would match the C behavior of treating 0 as false and everything else as true.

Boolean intStringToBoolean(numericBooleanValueString) {
  return !"0".equals(numericBooleanValueString);
}
2

As a note ,
for those who need to have null value for things other than "true" or "false" strings , you can use the function below

public Boolean tryParseBoolean(String inputBoolean)
{    
    if(!inputBoolean.equals("true")&&!inputBoolean.equals("false")) return null;
    return Boolean.valueOf(inputBoolean);
}
1

Returns true if comes 'y', '1', 'true', 'on'or whatever you add in similar way

boolean getValue(String value) {
  return ("Y".equals(value.toUpperCase()) 
      || "1".equals(value.toUpperCase())
      || "TRUE".equals(value.toUpperCase())
      || "ON".equals(value.toUpperCase()) 
     );
}
1
  • oh, no, why not just use the new stringified switch(value.toUpperCase()), or even better, a HashSet? How many times do you plan to execute toUppercase? Sep 26, 2021 at 11:56
0

Java is strongly typed. 0 and 1 are numbers, which is a different type than a boolean. A number will never be equal to a boolean.

2
  • 2
    technically, in my implementation, they're strings! :p Anyway, coming from a php/javascript background I guess I just assumed that it could distinguish between 0 and 1, on and off, yes and no and true and false appropriately. Oct 12, 2011 at 1:11
  • Well, first.. what a variable currently is doesn't matter in this context since the whole goal is to convert to a boolean, from something else. Second, you're assuming they're numbers. If the origin is a query string, for example, it'll actually come in as a string just as much as "true" and "false" would.
    – Rikaelus
    Feb 23, 2015 at 3:27
0

I had the same question and i solved it with that:

Boolean use_vote = o.get('uses_votes').equals("1") ? true : false;
0

How about this?

boolean uses_votes =
  ( "|1|yes|on|true|"
      .indexOf("|"+o.get("uses_votes").toLowerCase()+"|")
      > -1
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.