Question:
Java Programming - How do I evaluate a string of arithmetic expression?
anonymous
2008-10-03 12:13:29 UTC
For example, I have: string x = "1+2+3". Is there any function in Netbean or any workaround that can evaluate and give me an output of 6?

Thanks
Five answers:
warsql
2008-10-03 12:36:03 UTC
If you are using java 6, then you can use a scripting language, such as javascript.
?
2016-10-25 11:00:34 UTC
I do believe i will application that tommorrow for the heck of it. If that is not any longer executed tommorrow, it will be executed by technique of the great of the week. that is a sweet practice problem there. i will get decrease back to you if i'm getting mine finished previously you've a sturdy answer. Odds are in spite of the actuality that you'll discover someone with one achieved. in case you want some tricks on the thanks to start up, i could: a million. study in each and every thing utilising a Scanner, and the subfunction .getNext(); 2. Then take the string and use .toCharArray(); 3. Get the size of the String, so that you recognize at the same time as to quit processing characters 3. Create a Vector76a4e4f974fd895a0a2598c1cee28b4 to carry your "stack", and create a integer variable suggested as "topOfStack" 4. Create a StringBuilder, and use .append(' '); to operate issues as you bypass via the nature Array you created 5. in case you run for the duration of an operator, examine that is priority, and if mandatory upload it into the vector utilising .upload(), in the different case upload it into your output string which will be in postfix form. 6. at the same time bypass right into a swap or set of if statements to finish the mandatory action (so that you procedure the infix form as you bypass, this kind you do not ought to rework decrease back from postfix to infix, do merely it concurrently (i could attempt this in a seperate function for company's sake). which could want to grant you a start up in case you opt on you want to application it, in the different case supply me a at the same time as and that i will pop decrease back in with one. ______________________ be conscious: I went about programming this at present (Day after my unique submit), and daggon guy, which could no longer a exciting project to application, lol. I planned out how i replaced into going to do it, and realized it replaced into going to suck up too a lot time, so i will ought to grant it a shot in a lengthy time period contained in the week after this Chemistry attempt.
anonymous
2008-10-03 12:17:26 UTC
Use a scanner to read all of the integers into variables, such as an array, then add them all together with a for loop.
Matthew
2008-10-03 12:21:22 UTC
This should do it:





class AddNumbers {





public static void main(String args[]) {

int x = 1 + 2 + 3;

System.out.print(x);

}}
anonymous
2008-10-03 12:25:27 UTC
This is not a simple thing to do. You will not be able to accomplish it, so don't even try.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...