JEP is a Java mathematical expression parser. This means that you can pass JEP a string of mathematical operations (which may also contain variables) to perform and get back an answer.

To start using JEP, create an instance of the parser:

JEP j = new JEP();

Then, if you plan on using functions such as sin or cos, you need to tell JEP to expect these by calling the parser's addStandardFunctions method:

j.addStandardFunctions();

You're ready to start parsing. Call the parseExpression method with the expression that needs parsing. In this case, we'll parse whatever is passed from the command line:

j.parseExpression(args[0]);

Now print the result:

System.out.println(j.getValue());

For example, if the expression was "75 * 4 / (3 * 100)", the output would be:

1.0

If you want to use variables in your expression, you must tell JEP to expect these variables by calling the parser's addVariable method:

j.addVariable("x", 0);

One thing to keep in mind if you decide to use JEP in your code is that JEP comes with one of two licences: GPL or commercial. Depending on the environment you plan on using it in, the licence type may be important to you.

Below is a complete example that parses the expression passed in as a command line argument.

import org.nfunk.jep.JEP;

public class JepTip {
public static void main(String []args) { JEP j = new JEP();

j.addStandardFunctions();

j.parseExpression(args[0]);

System.out.println(j.getValue()); } }

Visit the JEP website to get complete documentation and plenty of sample code.

Interpreting Java This was published in Interpreting Java, check every Tuesday for more stories

Related links

Comments

1

lihan - 24/09/08

Develop Adobe Flex Application for you

1. Our hourly rate is very very low.
2. We are top Adobe Flex outsourcing service provider.
3. We have 60+ full time Adobe Flex developers working at our Beijing office.
4. Our company was registered both in San Francisco and Beijing.
5. Our skill set is "Adobe Flex/AIR + C#/Java/PHP + SQL Database".
6. Visit http://www.busycode.com or http://www.busycode.jp , you will see Busycode Inc. is all about Flex developer, Flex coder, Flex programmer, Flex expert, Flex engineer, Flex specialist and Flex outsourcing service provider.
7. Busycode, Inc. http://www.busycode.com http://www.busycode.jp

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

lihan - 24/09/08

Develop Adobe Flex Application for you 1. Our hourly rate is very very low. 2. We are top Adobe Flex outsourcing service provider. 3. ... more

Log in


Sign up | Forgot your password?

What's on?