How to make a formula that is saved in string be calculated?

Asked

Viewed 33 times

1

The user can enter a formula in a String type

String formula = (100+20+10)/50*1;

What I need to do is take this formula and do the calculated code to issue a result.

I thought to do with conditions using if or switch case but wanted to know if there is a library in JAVA that does this kind of thing. I explained in a simple way but what I am doing involves saving this formula in a database and after that will contain values to be defined in blank within the formula.

  • And what is the formula of this operation?

  • It is up to the user to choose the formula.

  • You have to make a little parser: http://answall.com/q/113578/101. But luckily someone has already answered this in a generic way for any language. I will close as duplicate.

  • Your doubt is not very clear. If you just do the operation and display, just one System.out.println((100+20+10)/50*1).

  • Missed this question takes only part of what I ask @bigown luckily I found this other question here http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form that solves my problem. I appreciate your help, man.

  • @Diegof, it might look like a simple exercise because I’ve simplified as much as possible and it might even be if it were a simple calculator, plus this one for a slightly more complex formula that involves values that are caught depending on what the person types in the formula, type String formula = 2+Pesodophospharo; this weight of the phosphorus is saved in a database and can be modified at any time, not to mention that the user of the system has released to create his calculation formulas in the way he wants. the more I’ve managed to solve my problem thanks.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.