James James - 2 months ago 7
Java Question

How to Display Boolean Value?

public static boolean isValidElement(String token) {
int a1 = 214;
if(a1 >= 0 && a1 <= 255){
return true;
}
else

return false;
System.out.println(isValidElement);

}


This is my code, im just not sure how to display the boolean result of true in this configuration. Im a bit new to java.
Any help is appreciated thanks!

rD. rD.
Answer

your both if and else part have return statements. It means that the pointer will return from this function on either one return statement and so it wont ever reach your System.out.println() statement.

Instead, you should write your System.out.println() before return statement.

Here's the code,

public static boolean isValidElement(String token) {
    int a1 = 214;
    if(a1 >= 0 && a1 <= 255){
        System.out.println(true); 
        return true;
    } else {
        System.out.println(false);  
        return false;
    }
}
Comments