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


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.

Boolean (Java Platform SE 8 ), parseBoolean. public static boolean parseBoolean(String s). Parses the string argument as a boolean. The boolean returned represents the value true  Description The Boolean.parseBoolean(String s) java method is used primarily in parsing a String method argument into a Boolean object. The Boolean object is a wrapper class for the boolean primitive data type of java API.


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

Java.lang.Boolean.parseBoolean() Method, Boolean. parseBoolean(String s) 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". The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean. In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean.


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

Boolean parseBoolean() method in Java with examples , The parseBoolean() method of Boolean Class is a built in static method of the class java. lang. Boolean which is used to convert a given string to its boolean value. Parameters: It takes one parameter value of type string which contains the value which is to be converted to boolean. The parseBoolean() method of Boolean Class is a built in static method of the class java.lang.Boolean which is used to convert a given string to its boolean value.


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;

Boolean.parseBoolean("1") = false?, 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"));. The parseBoolean() method of Boolean class returns the string argument as a Boolean. The value is returned true if the argument is equal to the string "true" otherwise, for null or any other string argument, it returns false.


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

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

Java Boolean parseBoolean() Method with Examples, The parseBoolean() method of Boolean class returns the string argument as a Boolean. The value is returned true if the argument is equal to the string "true"  Converts the specified span representation of a logical value to its Boolean equivalent. Converts the specified string representation of a logical value to its Boolean equivalent. Converts the specified span representation of a logical value to its Boolean equivalent. A span containing the characters representing the value to convert.


Java Boolean parseBoolean() method example, The parseBoolean method of Boolean class is one of the newest addition as of Java 1.5. This is to take into consideration the instances wherein  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Boolean.parseBoolean(“1”) = false…?


java.lang.Boolean.parseBoolean java code examples, The parseBoolean() method returns an equivalent boolean value of given String, for example, if you pass "true" it will return the primitive boolean value true. 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". Otherwise, a false value is returned, including for a null argument. Example: Boolean.parseBoolean("True") returns true. Example: Boolean.parseBoolean("yes") returns false.


Java, Parses the specified string as a boolean . C# Copy. [Android.Runtime.Register("​parseBoolean", "(Ljava/lang/String;)Z", "")] public static bool ParseBoolean  The parseBoolean() method converts string into boolean primitive. The parseBoolean() is the static method of Boolean class. The signature of parseBoolean() method is given below: public static int parseBoolean(String s)