0

I'm working on a java program, and one of the tasks I desire it to complete is converting a user given input into an equation that the program will solve. An example would be "(10 + 5 / 3) 2 - 3 (10 * (8 ^ 4))". It has to be able to solve secondary education level equations, so exponents, exponential notation, and possibly even variable solving are required.

I've looked around stackoverflow and found two things, third party API's and the ScriptEngine.

I have problems with both of these, as I don't desire to use any third party API's and the script engine's eval() function, at least to my what I've read and understand, is limited in it's capabilities.

Is there any other native API for doing something like this? Some trick I'm missing?

So, to recap, I want something native to java, that is not the script engine, that can solve secondary education level equations and expressions.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Sounds like an ambitious and rewarding venture! Are you interested in actually coding the program yourself? If so, perhaps start putting down in words how'd you approach solving `(10 + 5 / 3) 2 - 3 (10 * (8 ^ 4))`. – Meesh Sep 24 '13 at 03:11
  • ANTLR might be useful: http://www.antlr.org (but probably not as much fun as parsing the expressions yourself ;-) – dnault Sep 24 '13 at 03:19
  • If I have to get into writing my own string parser, then I will, but whats the point of reinventing the wheel that I just might not be finding. –  Sep 24 '13 at 03:27

2 Answers2

2

Java has built-in (javax.script.*) JavaScript engine:

It allow you to solve equations like these.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
msangel
  • 9,895
  • 3
  • 50
  • 69
  • OP doesn't want to use the script engine. – dnault Sep 24 '13 at 03:18
  • @dnault, but this is built-in. Otherwise he will have a lot of work with string parsing, replacing and so on. Desire not to use script engine mean only that OP is want to play with programing, but not to solve task. So I just put this answer for other visitors. – msangel Sep 24 '13 at 03:22
  • Does it support exponents, exponential notation, and exponents? If it does - I might consider using it, if it doesn't, I'm not even touching it. I also mentioned I didn't want to use the script engine. If I have to get into writing my own string parser, then I will, but whats the point of reinventing the wheel that I just might not be finding. –  Sep 24 '13 at 03:27
  • @Michaelwm, there are a LOT of math javascript libraries, that do the trick with math. As far as this engine is support javascript standard - most of than should work. FYI, in javascript exponential notation work out of box. – msangel Sep 24 '13 at 03:32
  • I guess I underestimated the scriptengine. I'll look into it, but I might just end up writing my own string parser one day when I'm bored. Thank's for the answer, I'll be leaving the answer up in case someone comes up with something better that's natively supported. –  Sep 24 '13 at 03:35
  • 1
    @Michaelwm, writing such parser is one of thing that every programmer should do once in his life. I wrote such one a lot of days ago. – msangel Sep 24 '13 at 03:51
2

..An example would be "(10 + 5 / 3) 2 - 3 (10 * (8 ^ 4))". ..

Try visiting this ScriptEngine demo...

enter image description here

I've looked around stackoverflow and found two things, third party API's and the ScriptEngine.

I have problems with both of these, as I don't desire to use any third party API's and the script engine's eval() function, at least to my what I've read and understand, is limited in it's capabilities

That understanding is incorrect.


See also

Examples using ScriptEngine.

  1. EvaluateString.java.

  1. SwingCalculator.java (because it is so pretty)

Calculet input = Calculet output

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433