Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Evaluating boolean string expression

 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Suppose I have the following saved in a string variable:



and I want to evaluate this expression. Is there a simple way for Java to input this string, parse it, and evaluate the expression? Or will I have to build a custom parser to evaluate this?

Thanks,
Dave
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to do it yourself -- or use an existing library that does it. There are a number of Java scripting libraries available. One is called "BeanShell", but there are others. Also check out "Groovy".
 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...after looking at BeanShell it may do exactly what I need by calling BeanShell from within my application.
 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used BeanShell's "Interpreter" class to do exactly what I needed. Thanks for the reference.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
alexandre brillant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also JFormula which is more compact and faster, you can plug any functions or symbols easily :

http://www.japisoft.com/formula/index.html

You can write (true&&true)||false but also ( true and true ) or false

Best regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic