Java expression parsers are lately showing a tendency to become non-free (at least in the paid sense) but help has arrived in Java 1.6 in the form of support in the runtime for JavaScript. This makes evaluation of a mathematical expression very easy indeed, as JavaScript's eval can be called. The source code is HERE (make sure you single-quote the entire expression for command line execution)
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Calc {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: java Calc <expression>");
System.exit(1);
}
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
System.out.println(engine.eval(args[0]));
}
}
import javax.script.ScriptEngineManager;
public class Calc {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: java Calc <expression>");
System.exit(1);
}
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
System.out.println(engine.eval(args[0]));
}
}