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.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Jose S
  • 620
  • 3
  • 8
  • 22
  • 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 http://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. – drubin Mar 25 '11 at 14:02

6 Answers6

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.

Ishtar
  • 11,542
  • 1
  • 25
  • 31
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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
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);
}
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
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);
}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2

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

return (str!=null && str.trim().equals("1"));
y0rk
  • 396
  • 1
  • 8
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.

Community
  • 1
  • 1
AechoLiu
  • 17,522
  • 9
  • 100
  • 118