Solve an equation given a String
807588Mar 26 2009 — edited Apr 29 2009Hi all,
I'm creating a method that I could pass a String, such as "(5 + 7) / 3" and it would be able to pass back a result, in this case "4".
Eventually I'd also like to add simple variable substitution, so I can pass in "(5 + 7) / X", and tell it X = 3, and have it return "4".
I've just started, and I can see several ways forward, but it seems like it's going to be quite complex. It also seems like this must be something that people have done in the past -- I feel like I'm reinventing the wheel here.
Does anyone know of any examples or libraries that can be used to do this? Maybe I could even use reflection in some way to get Java to simply interpret the result, exactly as if I had typed it in.
Any thoughts?
Thanks!
Tim