Java code…
If I have
int num = 10 + 5;
System.out.println(num);
The result is 15
And if I have
String str = "10";
int number = Integer.parseInt(str);
System.out.println(number);
The result is 10
But if I have
String str = "10 + 5"; //here the problem.
int number = Integer.parseInt(str);
System.out.println(number);
The result is error message.
How to do it correctly. What I want to implement is to take expression from a String and calculate it. I am forced to take it from a String because I take it from JTextfield, so I want to make the calculation for the returned String expression.
There is no simple way to do it directly because you need some sort of “expression evaluator”. One way would be to use a javascript engine from the scripting library: