-5

I'm trying to figure out how to parse an input string that contains a math equation given by the user like 2x^2+3x+6 to a double that operates the String with its variables that I will define later on with a for loop.

Basically,

for(x=0;x<=100;x++){
    String equation = "2x^2+3x+6";
    double y = equation;
    System.out.println("Your point is x: " + x +" | Y: " + y);
}

I found this old post but the BeanSheel Library only allowed me to use actual numbers without parameters like "x".

So what I'm trying to achieve is a program that will draw a function given by the user. But for the drawing part I'm fine

Community
  • 1
  • 1

1 Answers1

0

You could replace every occurrence of x within the equation string with the string representation of x's value; then you could use the post you linked to to evaluate it.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • excuse me , i have a question , for this problem there are two solutions , 1- using `replace` method 2-`split` and rebuild the String using Stringbuilder , which one is efficient ? – Mohsen_Fatemi Jan 23 '17 at 19:15
  • Try both and see. I'd be worried about getting it to work first. – Scott Hunter Jan 23 '17 at 20:36