Search code examples
java-memathematical-expressions

How to evaluate the mathematical expression from the string in J2ME


How to evaluate the mathematical expression from the string in J2ME.

For example a string containing "2*4".From the string i want evaluate the mathematical expression and get the result as 8 (2*4).How should i done this in J2ME.

In "Java™ Platform Standard Ed. 6 " there is a package namely "javax.script" is used for evaluating mathematical expressions from string.Like that any package or any class is available for J2ME to evaluate the mathematical expression from the string.


Solution

  • The following are the steps are used to evaluate the mathematical expression from the string in J2ME

    1. Read the expression in a string variable.Here the expression is in infix form.
    2. Convert the expression in the string variable into a postfix form and store it in a another string variable namely "postfix_str".
    3. Evaluate the expression in the postfix form (The value in the "postfix_str" data) using the Stack.