1

I have a string like ((TRUE && TRUE) || (TRUE && FALSE)) which I want to convert to boolean. How can I achieve this?

I tried using Boolean.parseBoolean(String) and Boolean.valueOf(String) but I think it works only if the string is as expected like 'True' only

9
  • Have you tried something already? Commented Aug 4, 2015 at 13:57
  • Correcting the string -- "((TRUE && TRUE) || (TRUE && FALSE))" Commented Aug 4, 2015 at 13:57
  • ya, I tried using Boolean.parseBoolean(String) and Boolean.valueOf(String), but I think it works only if the string is as expected like 'True' only Commented Aug 4, 2015 at 14:00
  • Google for an expression parser. Commented Aug 4, 2015 at 14:00
  • 1
    This question is duplicate of stackoverflow.com/questions/1432245/…strong text Commented Aug 4, 2015 at 14:05

2 Answers 2

3

You can "abuse" the similarity with JavaScript and use a standard available scripting engine:

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript"); 
    String expr = "((TRUE && TRUE) || (TRUE && FALSE))".toLowerCase();
    boolean result = Boolean.valueOf(engine.eval(expr));

Not sure whether lowercase and valueOf is right. Maybe a (Boolean) case will do.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks and it worked. Now, I have another query say I have a string like '((A&&B)||(A&&C))', then how can I evaluate as an boolean expression?
engine.put("A", Boolean.TRUE); (or just true).
This would be fine if the syntax is proper. The actual problem comes when the user gives invalid syntax like '((A&&B)||(AA&&C))'. If we defaultly set the value of A to true, the second part of the statement will not be evaluated and result would be true. The issue would be resolved if we set the value of A to false, but fails in another invalid expression like '((A&&B)||(A&&CC))' in which the CC would not evaluated.
I take it you do not want the lax handling of undefined in JavaScript. One can implement ones own variable binding: setBindings/createBindings and then throw an exception or such.
1

you need to build an expression parser. First of all, you need to decide on what the syntax of what your allowed epressions are. From your syntax above it looks like C. You should be able to find abundant examples of parser for C expressions by just Googling. Of course, you can also decide to write one by hand. when parsing a language you can chose to use a generator such as ANTLR or the more traditional LEX + YACC, in which case you will provide a grammar-based description of your language and code generators will spit out the program that parses such language. You will then incorporate such generated code in your program. For well known languages such as C and JAVA those solutions already have pre-cooked grammars, although you might have to extract just the expression part of it.

If the expression is in a traditionally interpreted language such as Python or Javascript, you can opt to incorporate an interpreter in your java program and feed it the expression instead.

Finally, if your expressions are in Java syntax and you're flexible to use an early version of Java 9, you may be able to tap into the new Java Shell feature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.