1

I am having trouble converting a String with operators to an Int, for instance:

String str = "8+8*2";

I want to convert it to an Int that will display the number 24.

Is this possible? I must add that it's for an Android application, if it means anything.

Thanks a lot!

Erez Hod
  • 11
  • 2
  • 1
    Yes, it is possible. :) Oh you probably want to know how? You'd need to write at least a simple expression parser, parse the expression into an expression tree or structure, then write a method to execute the expression structure. I'm sure you can find some info on it with Google. The complexity will depend upon how complicated an expression you want to be able to execute. For example, are you allowing parentheses? What math operations are allowed? Etc. – lurker May 22 '13 at 18:07
  • Well, it's just a simple calculator, I'm allowing +, -, * and / only. I'm not sure how to do what you just said lol. – Erez Hod May 22 '13 at 18:17
  • Ok no parentheses or anything? What about operator precedence? Since you say the value is 24, that means you want it to do multiplies before adds. So you can't do a linear parse of the input. You have to build a structure that sorts out the higher precedence operations (* and /) from lower (+ and -). If you don't know what I'm saying try Google. Look for "arithmetic parse trees" or something like that. – lurker May 22 '13 at 18:25

0 Answers0