-4

I have a string like 3x^2+5X+7 and what I want to do is, get x value from the user and make the program solve this equation. I couldn't figure it out how I am gonna do this so far. I would be really glad if you help me.

Erdinc Ay
  • 3,224
  • 4
  • 27
  • 42

1 Answers1

4

You could try this:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
int x = 42;
String foo = "3*x^2+5*x+7";
String input = foo.replaceAll("x", x);
System.out.println(engine.eval(foo));

(How to evaluate a math expression given in string form?)

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60