Home » Java » java – Convert String to boolean only if input is "true" or "false"-Exceptionshub

java – Convert String to boolean only if input is "true" or "false"-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

How can I make sure that the input of a String is either “true” or “false” and then parse it to a boolean?

For example:

String input = "true"; -> boolean result = true;
String input = "false"; -> boolean result = false;
String input = "foobar"; -> throw new InvalidInputException("true or false is allowed");
How to&Answers:

Try some thing like this :

String s = input.toLowerCase();
Boolean b ;
if ("true".equals(s) || "false".equals(s)) {
   b= Boolean.parseBoolean(s);
}
else  {
    throw new InvalidInputException("true or false is allowed");
}

I hope this helps

Answer:

You’ll have to do if else statements if you want to make absolutely sure that the input is either “true”/”TRUE” or “false”/”FALSE”:

if ("true".equalsIgnoreCase(input) {
    return true;
} else if ("false".equalsIgnoreCase(input)) {
    return false;
} else {
    throw new InvalidInputException("true or false is allowed")
}

If you literally only want the case sensitive “true” or “false” then change .equalsIgnoreCase() to .equals()

Alternatively you can use BooleanUtils in Apache Commons Lang
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/BooleanUtils.html

 Boolean bool = BooleanUtils.toBooleanObject(input)

If it’s neither “true” or “false” (Amongst other things like “t”, “y”, “yes” etc – see JavaDoc) then null is returned

Answer:

A little bit more modern (requires --enable-preview option to compile/run):

var result = switch (input) {
    case "true"  -> true;
    case "false" -> false;
    default      -> throw new InvalidInputException ("true or false is allowed");
};

for case insensitive convertion:

var result = switch (input.toLowerCase()) {
    case "true"  -> true;
    case "false" -> false;
    default      -> throw new InvalidInputException ("true or false is allowed");
};

Note: eventually missing check for null

Answer:

From java doc about method Boolean.valueOf(String):

Returns a Boolean with a value represented by the specified string.
The Boolean returned represents a true value if the string argument is
not null and is equal, ignoring case, to the string “true”.

So you can use it in this way:

public static String getBoolean(String s) {
    if (!s.equals("true") && !s.equals("false")) {
            throw new InvalidInputException("true or false is allowed");
    }
    return Boolean.valueOf(s) ? "true": "false";
}

Answer:

Java 8:

public static boolean parseBoolean(String value) {
    return Optional.ofNullable("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value) ? Boolean.parseBoolean(value) : null)
            .orElseThrow(() -> new InvalidInputException("true or false is allowed"));
}

Answer:

You can use:

if(input.toLowerCase().equals("true") || input.toLowerCase().equals("false")) {
    return Boolean.parseBoolean(input);
} else {
    throw new InvalidInputException("true or false is allowed");
}