Ho scoperto che esiste un' utilissima libreria:
da http://commons.apache.org/downloads/index.html scaricate le librerie
-JEXL
-Logging
dell'ultima vi servirà solo "commons-logging-1.1.1.jar"
nel programma includete
codice:
import org.apache.commons.jexl.*;
ho scritto questa classe di esempio:
codice:
public class Prova {
public static void main(String argv[]) throws Exception{
JexlContext jc = JexlHelper.createContext();
Integer i = new Integer(2);
Integer j = new Integer(3);
jc.getVars().put("y", i);//associa la variabile i con la y
jc.getVars().put("x", j);
Expression e = ExpressionFactory.createExpression("x*y+4");
Object ris = e.evaluate(jc);
int r = Integer.parseInt(ris.toString());
System.out.println(r);
}
}