user12881831
user12881831

Reputation:

Convert String to boolean only if input is "true" or "false"

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

Upvotes: 3

Views: 8844

Answers (5)

user85421
user85421

Reputation: 29680

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

Upvotes: 1

Benkerroum Mohamed
Benkerroum Mohamed

Reputation: 1936

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

Upvotes: 2

dariosicily
dariosicily

Reputation: 4527

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

Upvotes: 1

tomgeraghty3
tomgeraghty3

Reputation: 1254

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

Upvotes: 8

Gaurav Dhiman
Gaurav Dhiman

Reputation: 1023

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

Upvotes: 0

Related Questions