I want to evaluate a sting math expression in java. This string should contain functions (avg, max, min, ...) applied to vectors or simple numbers. I already use ScriptEngineManager with javasript engine but it just use numbers. I also see symja lib but it look too complicated et not documented. How to do? Thanks
How to evaluate string math expression in java
1.3k Views Asked by Fridolin Amadou At
2
There are 2 best solutions below
0
Michael Couck
On
There are two very good expression parsers, JEP(paid now unfortunately - http://www.singularsys.com/jep/) and Jexl(much more than just an expression parser - http://commons.apache.org/proper/commons-jexl/).
I prefer Jexl, so here is an example:
JexlEngine jexl = new JexlEngine();
// The expression to evaluate
Expression e = jexl.createExpression("((a || b) || !c) && !(d && e)");
// Populate the context
JexlContext context = new MapContext();
context.set("a", true);
context.set("b", true);
context.set("c", true);
context.set("d", true);
context.set("e", true);
// Work it out
Object result = e.evaluate(context);
More examples - http://commons.apache.org/proper/commons-jexl/reference/examples.html
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in STRING
- SML - Find same elements in a string
- match hex string with list indice
- How can I determine the index of the same set of characters between two strings that are of different lengths?
- String.replace() isn't working like I expect
- How to do a case-insensitive string comparison?
- Trying to save an np array with string and floats, but getting a error
- String replace with integer not working
- How to calculate a length of array with out using library
- Java replace every Nth specific character (e.g. space) in String
- Split the strings into two parts Python
- Perl Regex: Merge multiple one-character substrings
- Squid S2275 does not know about format string argument indexes
- more efficient way of remove a few characters from the end of a string
- python member str performance too slow
- String.split() not behaving in android
Related Questions in MATH
- bc: prevent "divide by zero" runtime error on multiple operations
- How to round smoothly percentage in JS
- Calculate if trend is up, down or stable
- How to pick a number based on probability?
- Python 2.7 - find combinations of numbers in a list that add to another number
- How to translate an object to a location slowly (so that it can be seen)
- max() implemented with basic operators
- Matlab: how to fit time series with a funcion of a certain type
- 3D B-Spline approximation
- Issues with adding doubles. Arithmetic Coding
- Calculate new position post rotation
- Javascript: PI (π) Calculator
- How to compute a^^b mod m?
- Need Custom Query in SQL Server
- Number of divisiors upto 10^6
Related Questions in SCRIPTENGINE
- Java Scripting API -- Property from JavaScript object is always null
- How to write a java script code in a html page by a java program at run time?
- Why AccessController is not blocking the non-privileged access
- Error while using Script Engine manager to evaluate Java String code
- Passing multiple arguments to java function via scriptEngine in javascript
- access javascript method from java side
- How can I evaluate my own Groovy script from Java?
- Java ScriptEngine with multiple Threads and Lock
- Using Java ScriptEngine to perform multiple evaluation synchronously
- MSScriptControl.ScriptControlClass - access a sub-object of the main object
- Returning output value from JavaScript code in Java using Nashorn
- How to resolve No ScriptEngine found for language js?
- Setting "Callable" on a Java object from within Nashorn script
- javascript execution failing in java with XPathResult undefined
- Decoding Base64 String in Java
Related Questions in SYMJA
- How to solve multivariate equation systems programmatically?
- Proper symja evaluation?
- How to solve inequalities with Symja?
- symja: quadratic inequation and modulus
- How to fix precision in Symja?
- Java Equation Evaluation
- Symja Jar Algebra
- How does the library "Symja" need to be imported with Gradle in order to work with the Elasticsearch server module?
- How to evaluate string math expression in java
- Android dependency Symja will not load in gradle
- Gradle build stuck while using symja library
- App crashes when using Symja library for symbolic differentiation of a user input function. (Using Android Studio)
- Abs[x] not recognized in symja math parser
- Gradle build slow after adding symja and log4j-1.2.11 to Android studio
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Take a look at the Math and String classes of the javadoc. If you know the format of the string, you should be able to search through it to find the specific numbers and functions you're using. If you are only using one of the avg/max/min per input, it should be pretty easy.
Here's an example, lets say you want it formatted like so (it's easy if theres a comma after every value):
"FUNCTION(a, b, c,)" -> "MIN(3,6,8,)"
The first thing you want to do is have it figure out which function you're doing. Using the indexOf method, we can figure out if it contains MIN or MAX or whatever.
You'll also need to create a list of all the numbers you're using.