Home > Enterprise >  How to validate exact value of True and False using Boolean in Java?
How to validate exact value of True and False using Boolean in Java?

Time:11-02

The requirement is to get an error when the activeState value given by the user is not true or false. If the user gives true or false, the code should give "passed" as output and any other input other than true or false should give "failed" as output.

Here is my code-

...
public static boolean isActive(Boolean activeState){

    boolean flag=false;

    if(activeState !=null && (activeState.equals(true)||activeState.equals(false)))

    {

       flag=true;

    }

    return flag;

}

...
activeState                     Output                                

 true                           passed                   

 false                          passed                  

 null                           no output                   

 true#                          no output                  

 trrrrue                        no output                 

 @false                         no output  
              
 Lucas                          no output 

As per my observation the code is giving required output only when the user gives valid input ie. True or False. But when any other input value is being given ie. null,truuee,false#; nothing is coming as an output. It is neither giving any error nor any output, just blank. No response is getting generated.

I have checked out solutions where by changing the datatype of the input field fixes the issue, but I can't change the data type of activeState from Boolean to String. Is there any way to validate the activeState so that it can validate all the input values and generate output as required without changing the datatype?

CodePudding user response:

Simple null safe boolean check method. This will return the value of the boolean if b is not null. If b is null, return false.

boolean nullSafeBoolCheck(Boolean b) {
    return Optional.ofNullable(b).orElse(false);
}

CodePudding user response:

You can not pass anything other than true, false, or null as an argument if you keep the parameter as a boolean.Only opting to use string will solve your issues

CodePudding user response:

A primitive boolean has two possible states:

  • true
  • false

Those are not text values, not String. Those values represent “trueness” and “falseness” concepts.

The Boolean (notice the uppercase “B”) class wraps a value of the primitive type boolean. An object reference of this type may point to no object. Therefore a Boolean may evaluate to any of three possible values:

  • true
  • false
  • null

So given your requirement stated in your first paragraph, your entire method could consist of this ternary:

public static String isActiveStateValid ( Boolean b ){
    return Objects.isNull( b ) ? "Failed" : "Passed" ;
}

CodePudding user response:

So, Boolean value can have only three values: null, true, false. So without a double you can rewrite your code with:

public static boolean isActive(Boolean activeState){
    return activeState != null;
}

To check user output in Spring you should handle the exception. E.g. if you in the controller layer, you can define a method anotated with @ErrorHandler(exception=Exception.clsss) smth like that.

I would give you more presice answer if you provide a related code to make me cleare how user proved the incorrect value (is this a REST call)?

CodePudding user response:

public static String validateIsActive(final String userInput) {
 return new Boolean(userInput) ? "passed" : "failed";
}

Because the used constructor of Boolean only returns Boolean.TRUE, if the argument is not null and equals (ignore case) "true".

  • Related