java-me: Convert String to boolean
Asked Answered
B

6

14

I'm developing for BlackBerry and I got stuck with this stupid problem:

I need to convert string values "1" and "0" to true and false, respectively. Nevertheless, Blackberry JDK is based in Java 1.3, so I can't use Boolean.parseBoolean, Boolean.valueOf or Boolean.getValue.

Obviously I can do something like:

if (str.equals("1")) return true;
else if (str.equals("0")) return false;

But this looks very ugly and maybe these string values could change to "true" and "false" later. So, Is there another way to convert between these types (String -> boolean, Java 1.3)?

UPDATED: all the answers of this question was very helpfully but I needed to mark one, so I selected Ishtar's answer.

Even so, my fix was a combination of multiple answers.

Binding answered 25/3, 2011 at 5:10 Comment(1)
This question leads me to the DailyWTF article. I am not insulting you just merely suggesting that Boolean values changing should be a very very rare occurrence read thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx keep it simple even if you do hard code the "1" or "true" in the function it is ok. Somethings are just certainties else they would never have made it into the core language.Waldgrave
B
14
public static boolean stringToBool(String s) {
  if (s.equals("1"))
    return true;
  if (s.equals("0"))
    return false;
  throw new IllegalArgumentException(s+" is not a bool. Only 1 and 0 are.");
}

If you later change it to "true/false", you won't accidentally order 28,000 tons of coal. Calling with the wrong parameter will throw an exception, instead of guessing and returning false. In my opinion "pancake" is not false.

Baklava answered 25/3, 2011 at 16:48 Comment(0)
E
9

If you don't have Boolean.valueOf(String s) ... yeah, that's pretty much it. I'd define your own static method like:

public static boolean booleanFromString(String s)
{
    return s.equals("1");
}

That would solve your "May change to true or false later" problem as you could add/change that in the method and not have to change anything else in your code.

Elizaelizabet answered 25/3, 2011 at 5:24 Comment(0)
E
2

maybe these string values could change to "true" and "false" later

Don't hard code your parameter.

So define your own method like this.

public static final String TRUE = "true"; //"1"

public static boolean strToBool(String s) {
    // don't hard code your parameter.
    return str.equalsIgnoreCase(TRUE);
}
Eigenvalue answered 25/3, 2011 at 5:32 Comment(5)
Actually.. this is one case where I would hard-code my parameter as the definition of a "true" string is a (or a set of) very particular value(s). +1 though.Sevigny
@pst: you are right. but the OP's case is different. see --> string values could change Eigenvalue
@John Then I'd change them in the method ;-)Sevigny
@pst: again you are right. so the answer is to encapsulate it into the method.Eigenvalue
@Brian: change the method body. or split the method into two. anyhow, document it.Eigenvalue
V
2

Java's Boolean object (if I remember correctly) has already 2 constants:

  • Boolean.TRUE
  • Boolean.FALSE

you can use Boolean.booleanValue() to return it's corresponding boolean value.

You can create your own valueOf(String s) method to return a boolean and/or Boolean like so:

public static boolean toBoolean(String s) {
    return ((s != null) && s.equalsIgnoreCase("true"));
}

public static Boolean valueOf(String s) {
    return (toBoolean(s)? Boolean.TRUE : Boolean.FALSE);
}
Vanhorn answered 25/3, 2011 at 6:27 Comment(0)
L
2

you should check null and whitespace chars. remove them and check value.

return (str!=null && str.trim().equals("1"));
Lonely answered 25/3, 2011 at 14:1 Comment(0)
T
0
public static boolean stringToBool(String s) {
        s = s.toLowerCase();
        Set<String> trueSet = new HashSet<String>(Arrays.asList("1", "true", "yes"));
        Set<String> falseSet = new HashSet<String>(Arrays.asList("0", "false", "no"));

        if (trueSet.contains(s))
            return true;
        if (falseSet.contains(s))
            return false;

        throw new IllegalArgumentException(s + " is not a boolean.");
}

Enhance Ishfar answer.

Tanny answered 10/12, 2015 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.