# Thread: Convert A String To Math Problem

1. ## Convert A String To Math Problem

I figured it out thanks!
Last edited by anthropamorphic; 07-06-2011 at 05:42 AM.

2. Are the spaces there in every string? If there was you could string.split your string into an array. Then you would just have to figure out which ones were numbers and which ones were symbols.

Take a look at the api: String (Java Platform SE 6)

3. Hmm, that is definitely a more simple approach. I guess I learned something today

4. If you cannot use Javascript, what you need to do is convert your infix (2 + 3) expression into a postfix (2 3 +) expression. Then you can evaluate the expression using a Stack. For each operand (number) push onto Stack. For each operator (+-*/etc) pop off 2 values from the Stack, perform operation, push result back onto Stack. Once you are done the only value left on the Stack should be the result. If you Google you should find plenty of info on how to do this.

5. what you need to do is convert your infix (2 + 3) expression into a postfix (2 3 +) expression.
If you need to make a parser this is A possibility (and probably the easiest to program). But you can also make other (prefix I guess) parsers. I did it myself, is was quite a project but it works great, and is far easier for the users (including me).
It is one of the projects I still borrow from the most in other projects (currently a matrixcalculator: I can just use sin2pi or sin(2pi) in my matrix which is much more palatable then 2 pi * sin (or something like that)).
Last edited by Jodokus; 07-05-2011 at 01:57 AM.

6. Thanks for all the suggestions I'll try them out and hopefully I will be able to figure it all out. (I'm not the best at java yet)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•