Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the package exp4j always returns UnknownFunctionExpression and UnparsableExpressionException

Tags:

java

I have to parse a string which contains a formula for my java project and I wanted to use the package exp4j (http://www.objecthunter.net/exp4j/index.html) to do so. But I always get the same error. Its probably really stupid, but sadly I'm missing it. So here is, what I tried:

I simply tried this for testing:

import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;

public class Tester {
  public static void main (String[] args){
    Calculable calc = new ExpressionBuilder("3+2").build();
  }
}

Now eclipse tells me Unhandled exception type UnknownFunctionExpression and Unhandled exception type UnparsableExpressionException.

After that I tried just using one of the examples on the mentioned website:

import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;

public class Tester {
  public static void main (String[] args){
    double varX = 2;
    double varY = 3;

    Calculable calc = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)")
      .withVariable("x", varX)
      .withVariable("y", varY)
      .build();
    double result1=calc.calculate();
  }
}

resulting in the same exceptions as above.

Sorry for any bad english, I'm german and thanks for any help :)

like image 533
user1797862 Avatar asked Feb 20 '26 14:02

user1797862


1 Answers

The error tells you that a checked exception might occur, so you have to handle it yourself.

you could either use try/catch to handle it or just declare the exception using throws clause

public static void main (String[] args) throws UnparsableExpressionException{

    Calculable calc = new ExpressionBuilder("3+2").build();

}

or you can wrap the code in a try/catch to handle the exception.

try {
    Calculable calc = new ExpressionBuilder("3+2").build();
}
catch(UnparsableExpressionException ex){
ex.printstacktrace();
}

follow the same for UnknownFunctionExpression as well. and read about Exceptional Handling in java here

like image 95
PermGenError Avatar answered Feb 22 '26 04:02

PermGenError



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!