0

Simple Question, maybe not so simple Answer: I got a String, for example "bool1 OR (bool2 AND bool3)", and also the required booleans, for example in a Map. Is there an easy way to get the boolean of the String equation? It should (in the best case) be able to handle also more "complex" strings, not just like this 3-variable thing.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
Nico770
  • 45
  • 5
  • 1
    The easiest would be to use some embeddable language. JavaScript, for instance or any other. Literally dozens out there. – lexicore May 07 '18 at 19:49
  • What you're asking for is a Java method to evaluate a string that contains a boolean expression. It's a great problem for learning to program. I doubt there's a built-in solution. – Jeff Learman May 07 '18 at 19:50
  • Ah, we do have https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form . That uses standard Java operators (`&&` and `||`). See also: https://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java – Jeff Learman May 07 '18 at 19:55
  • you can look into learning the concepts of syntax parsing which would help build a simple parser for your syntax that would in turn allow you to evaluate expressions. Search for ANTLR tutorials. It is a parser generator. Since its written in java, you can integrate a parser you create into your application. It might be an overkill to go this route but it will help understand the concept. – patelb May 07 '18 at 19:56
  • 1
    https://stackoverflow.com/q/12203003/2158271 is quite a similar question. See the answers for it. – haba713 May 07 '18 at 19:57
  • Already found this, too: https://stackoverflow.com/questions/19383953/is-it-possible-to-evaluate-a-boolean-expression-for-string-comparions seems like there was another similiar question, which i didn´t find earlier. – Nico770 May 07 '18 at 19:59
  • Should `!negations` or `==`, `!=` comparisons also be supported? If it's really just some strings with brackets, AND and OR, it shouldn't be that hard to create some small utility method for this. For everything beyond that, a proper parser is likely the easier choice. – Marco13 May 07 '18 at 23:35

0 Answers0