i want convert String to boolean, but Output is always false.
a=true;
b=true;
c=true;
String zOrginal="(a^b)|c";
boolean z= Boolean.parseBoolean(zOrginal);
out.println(z);
Output is always false,
i want convert String to boolean, but Output is always false.
a=true;
b=true;
c=true;
String zOrginal="(a^b)|c";
boolean z= Boolean.parseBoolean(zOrginal);
out.println(z);
Output is always false,
This is the source code for Boolean.parseBoolean
:
public static boolean parseBoolean(String b)
{
return "true".equalsIgnoreCase(b) ? true : false;
}
The only time this function returns true
is if the string passed to it is either "true" (case notwithstanding).
If you're looking to check whether the string is non-empty, use the String.isEmpty
method. If you're looking to actually "eval" the string, that isn't as straightforward... but you may want to read through Is there an eval() function in Java?.
You should evaluate the expression directly not within the String.
boolean a,b,c;
a=true;
b=true;
c=true;
boolean z= (a^b)|c;
System.out.println(z);
In Java, the parseBoolean
method parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”
.
So, if you parse "(a^b)|c"
you will get false
as it's not going to evaluate the expression.
If you want to evaluate the result of the expression "(a^b)|c"
where a
, b
, and c
are the variables defined above, just execute the expression directly as java code as stated in other answers.
boolean result = (a|b)^c;
However, if one really wants to evaluate a an expression coming from a String
, say when the string comes from user input, then there is a library called JExl that let's you do this:
http://commons.apache.org/proper/commons-jexl/
Add the following lines to your maven pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.1</version>
</dependency>
Haven't tested the code below, but you can give it a go.
import org.apache.commons.jexl3.*;
JexlEngine jexl = new JexlBuilder().create();
String expression = "(a|b)^c";
JexlExpression jExpression = jexl.createExpression(expression);
// JexlContext stores the values for variables used in an JexlExpression
JexlContext context = new MapContext();
context.set("a", true);
context.set("b", true);
context.set("c", true);
// evaluate expression.
Long result = (Long) jExpression.evaluate(context);
System.out.println(result); // 0 for false
Of course, the performance will be worse than executing the code directly because the expression have to parsed into an abstract syntax tree (AST) before it can be evaluated.
There are number of way to convert string to boolean:
Use Boolean.valueOf(StringVal) method:
public class Test {
public static void main(String[] args) {
String val = "false";
boolean result=Boolean.valueOf(val);
System.out.println(result);
}
}
In this case true is returned if and only if string is "true" in other case false is returned.
you can create your own method to check the string value and can return true or false based on it.
public class Test {
public static void main(String[] args) {
String val = "true";
boolean result=getBoolean(val);
System.out.println(result);
}
public static boolean getBoolean(String value)
{
return "true".equalsIgnoreCase(value) ? true : false;
}
}