2

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,

cs95
  • 379,657
  • 97
  • 704
  • 746

5 Answers5

3

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?.

cs95
  • 379,657
  • 97
  • 704
  • 746
3

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);
davidxxx
  • 125,838
  • 23
  • 214
  • 215
Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
3

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
Hemant Singh
  • 1,487
  • 10
  • 15
1

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.

Zaiyang Li
  • 102
  • 7
  • JexlEngine, JexlBuilder, JexlExpression, JexlContext and MapContext cannot be resolved to a type. plz help – Mojtaba Hajilari Jun 24 '18 at 06:02
  • Have you tried to add Jexl library to your dependency? Not sure which java build tool you use (Gradle, Maven, etc), but take a look at the maven repository for Jexl: [Link](https://mvnrepository.com/artifact/org.apache.commons/commons-jexl3/3.1). Also need to add `import` statement which i have edited my code – Zaiyang Li Jun 24 '18 at 10:44
  • i use eclipse. error after run, " JexlEngine jexl = new JexlBuilder().create(); " – Mojtaba Hajilari Jun 24 '18 at 12:12
  • do you see a file called `pom.xml` or `build.gradle` file anywhere in the project directory? – Zaiyang Li Jun 24 '18 at 12:43
  • you need to add jexl library to the dependencies in the `pom.xml` or `build.gradle` file. – Zaiyang Li Jun 24 '18 at 18:50
  • HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class org.apache.commons.jexl3.internal.Engine$UberspectHolder – Mojtaba Hajilari Jun 24 '18 at 19:14
0

There are number of way to convert string to boolean:

  1. 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.

  2. 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;
       }
    }
    
NullPointer
  • 7,094
  • 5
  • 27
  • 41