0

I am making an Android app that will eventually be used as a dice roller for a game. I have a keypad that enters an equation into a String. I then parse the String into an array and need to compute the equation. I am using a for loop to go through the array, but I'm not sure how to handle the multiplication when it comes up.

As an example:

String theEquation = "2 + 3 * 6";
String[] result = theEquation.split("\\s+");

So I now have an array that has 2, +, 3, *, 6 in it. How would I go about turning that in to the answer of 20?

I initially used a for loop to just add the entire array together. That works fine when it is just addition. But things get hinky when i add in multiplication.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
Inessaria
  • 79
  • 1
  • 2
  • 10
  • You need an equation parser. – Mark Jeronimus Aug 12 '16 at 11:13
  • This is a broad topic. You need to specify which kind of operations you want to support, what input values are allowed etc. Out of curiousity: Why do you need such a thing for a dice game? – J Fabian Meier Aug 12 '16 at 11:13
  • Check this: http://stackoverflow.com/questions/1432245/how-to-parse-a-mathematical-expression-given-as-a-string-and-return-a-number – beeb Aug 12 '16 at 11:14
  • It is for Dungeons and Dragons. I use it in place of the various different dice. As an example, I need to roll 5- 6 sided dice for one thing that i do often. I only have 1 actual 6 sided die. Its much easier to open this app and hit "5*d6" and have it compute the roll. – Inessaria Aug 12 '16 at 11:20
  • You could check if your result array contains * to make the operation in first place. Then remove from your array the elements before and after the * and continue with operations. – Manu AG Aug 12 '16 at 11:29
  • Fairly low tech and easy to implement.....Perfect for something like this. I will try that out, thank you. – Inessaria Aug 12 '16 at 11:33

2 Answers2

2

You can use javax.script.ScriptEngine

Output

20

See this code:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class EvaluateExpr {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager se = new ScriptEngineManager();
    ScriptEngine scriptengine = se.getEngineByName("JavaScript");
    String theEquation = "2 + 3 * 6";
    System.out.println(scriptengine.eval(theEquation));
    } 
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

You are asking to implement some kind of an interpreter basically interprets the simple math notations and gives true results.

If you really wanna develop a basic level interpreter on your own, you can find a very good sample written with Python at here (don't worry, python is very easy to read and understand).

The Python code at the link simply works like this:

$ python calc1.py

calc> 3+4

7

calc> 3+5

8

calc> 3+9

12

calc>

There is also another sample written in Java here.

Bu anyway, what you need is to somehow calculate the math given in String so @Raman's answer is very good for the solution of your problem.

Community
  • 1
  • 1
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61