Question

Im using the java scriptengine to evaluate the math of a string i have.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class GraphYPosAlg 
{
  public static double YPos(String funktion) {
    Double tempDouble;
    ScriptEngineManager StringToInt = new ScriptEngineManager();
    ScriptEngine engine = StringToInt.getEngineByName("JavaScript");
    tempDouble = engine.eval(funktion);
    return tempDouble;
  }
}

I get the error "required: java.lang.Double, found: java.lang.Object" I need to convert the engine.eval(funktion); into a double but how?!?!?!?

Please describe much im pretty new to programming.

thanks in advance.

Was it helpful?

Solution

You could invoke the toString() method on the object and create a double out of it:

tempDouble = Double.parseDouble(engine.eval(funktion).toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top