assume i have integer variables a,b and c.
c = a + b; or c = a - b; or c = a / b; or c = a * b;
As you can see, the computation operator needs to be passed dynamically at run time. So i have a jComboBox of operators, so a user will select either a +, -, * or / from the jcomboBox.
how do i get the jCombobox selectedItem(which will be either /, *, -, or + ) and use it in getting the value of c.
Eg. if the user selects *, then the expression should be c = a * b else if the user selected say +, then the expression should be c = a + b.
Here is a way to ‘cheat’. All the parsing is done by the
ScriptEngine, we just need to assemble the parts of the expression.See Also
javax.scriptpackageScriptEngineScriptEnginedemo. for exploring the capabilities of the JS engine.