I have a string
String numAsString = "8 * 9";
And I want to convert this into an integer, so when I print the integer
int numAsInt = (the code I am asking for);
System.out.println(numAsInt);
The output would be
72
I have tried parseInt and valueOf, but both give an exception cause of the asterisk.
If you don't know, the asterisk means multiplication.
Is there a way to do this?
You can use something like this and it is easy:
String[] values = numAsString.split("\\*");
numAsInt = Integer.valueOf(values[0].trim()) * Integer.valueOf(values[1].trim());
but If you have a calculation or something with parenthesis, plus, minus and etc, you can use this:
Running JavaScript code in java
I think this help you.