Submit Article

If you would like to submit an article, click the button below.

Categories

<-- Back to Current Category

Math Parser for Java

By: David Dale, Tue Aug 26th, 2008

Java Programmer? Do you need to parse mathematical expressions in Java? If yes, I would like to tell you about JbcParser Mathematical Expression Parser to parse and evaluate math expressions in in your Java projects. Parsing formulas given as strings at runtime is a challenge for any programmer when he wants to allow the user input a mathematical formula. It is easy to compute mathematical expressions that are known at compile time. We type it in our program and when we execute our program, we get the result. For example x+sin(x) can be easily computed when the value of x is supplied. However, when the mathematical expression is given as a string at runtime, for example "x+sin(x)", it is no different then a meaningless "abc". The programmer has to make an explicit effort to make the program understand that there is a plus (+) sign that adds the value of x and the value of sin(x). Of crourse, the program also needs to understand the concept of a function call such as "sin(x)". The concept of breaking such an expression into it's conceptual sub-sections is called parsing. The programmer writes code which typically creates a data structure which represents the mathematical expression in a way that can be used to evaluate it efficiently. This is a gross simplification of the process but that is how it looks from a very high level, birds eye view. Java language, with it's expressive syntax and capable libraries and data structures makes it possible to implement a re-usable solution for the expression parsing problem. JbcParser - Math Parser for Java - simplifies the task for us by implementing the parse and evaluate algorithm. The expression is first parsed into an expression tree. Once this is done, the compiled tree structure is re-used with different variable values to achieve very fast evaluation performance. JbcParser Math parser is implemented in Java. It allowes the programmer set the expression as a string, create user defined variables, set their values, create user defined functions and get expression value with one function call. Most simple example looks like this: mathParser.setExpression("x+sin(x)"); mathParser.setX(3); mathParser.setY(2); double value = mathParser.getValue(); The programmer can implement his own functions in Java and hook them up to the Math Parser object so that they can be used in expressions all with few lines of code. The Math Parser reports errors via ParserException. This exception object carries out syntax errors that indicate invalid portion of the mathematical expression. Tackling all of the challenges of implementing a parser is almost impossible within the scope of a broader project where there are pressing requirements in terms of time and functionality to be delivered. Most of us need to focus on the actual business problem at hand and view the mathematical expression parsing aspect of the problem as a side issue. Thus, it becomes very very hard to deliver correct, united tested, proven code on time and in budget. Trying to code a expression parser could take an experienced developer a month to write and to test. When such as road block appears, the best way is to find a good component that can easily be integrated into your development project, leaving you free time to focus on other aspects of your project or to simply go home and rest. There is peace of mind in using a component that many others have used for many years and flushed out the bugs for you. JbcParser Java math parser is a well tested, easy to use, simple Java API that can save time and money for experienced or novice programmers alike. It comes with java soure code so you are never stuck with a black box.