Thomas Clayson
Thomas Clayson

Reputation: 29925

Boolean.parseBoolean("1") = false...?

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

Upvotes: 89

Views: 148388

Answers (11)

Max Spring
Max Spring

Reputation: 1140

How about this?

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

Upvotes: 0

Th&#233;o Moulia
Th&#233;o Moulia

Reputation: 127

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

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

Upvotes: 1

Yaro
Yaro

Reputation: 684

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()) 
     );
}

Upvotes: 1

AndroidDev
AndroidDev

Reputation: 21237

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

(o.get('uses_votes')).equals("1") ? true : false;

Upvotes: 4

Saqib
Saqib

Reputation: 1293

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;
}

Upvotes: 31

ryan0
ryan0

Reputation: 1525

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);
}

Upvotes: 3

kommradHomer
kommradHomer

Reputation: 4210

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);
}

Upvotes: 2

BalusC
BalusC

Reputation: 1108972

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.

Upvotes: 103

Steve Kuo
Steve Kuo

Reputation: 63094

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.

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54252

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;

Upvotes: 10

mellamokb
mellamokb

Reputation: 56779

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".

Upvotes: 14

Related Questions