开发者

java-me: Convert String to boolean

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

I need to convert string values "1" and "0" to true a开发者_如何学编程nd 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.


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.


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.


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


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


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

return (str!=null && str.trim().equals("1"));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜