Assuming that i have a string "2+2", how do i solve that? i got the numbers and sign separated but cant add'em because + is a string. Is there any way i can do this?
Printable View
Assuming that i have a string "2+2", how do i solve that? i got the numbers and sign separated but cant add'em because + is a string. Is there any way i can do this?
Use an if statement to test the value of the String to see what it is.
The code will have to have the math operations in it already and use the value of the String to determine which operation to do. There is no way to "compile and execute" a String as a java statement.
So your're saying it is impossible?
can u tell me what kind of math operationns?
To do addition use the +: x + yQuote:
kind of math operations
to do subtraction use the -: x - y
etc
Code :
if(str.equals("+")) // test the string val = x + y; // do the math operation else id(str.equals("-")) val = x - y;