0

this is my code at the moment:

public class Evaluation {

    String str1 = "1 + 2 + 3";

    public static double evaluation(String str1){
        if (str1 == null || str1 == ""){
            return 0;
        }

        return 0;
    }
}

What I want to be able to do it use str1 and evaluate whatever is put in there.

eg. "1 * 3 * -2" should return -6 etc. and it must be in the form of

Number, Operator, Number, Operator, Number

How do I accomplish this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sinn0n
  • 61
  • 2
  • 3
    1. Have you tried asking Google about it? 2. What code have you wrote in an attempt to solve the problem? 3. Do you want to implement this algorithm yourself or are you looking for a library that will do this for you? – Jaroslaw Pawlak Nov 12 '15 at 12:45

1 Answers1

1

Try ScriptEngine:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
String input = "1 + 2 + 3";
System.out.println(engine.eval(input)); //prints 6
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23