# How to convert from string to math sign

Printable View

• June 29th, 2012, 06:55 AM
usama8800
How to convert from string to math sign
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?
• June 29th, 2012, 07:04 AM
Norm
Re: How to convert from string to math sign
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.
• June 29th, 2012, 07:16 AM
usama8800
Re: How to convert from string to math sign
So your're saying it is impossible?
• June 29th, 2012, 07:48 AM
usama8800
Re: How to convert from string to math sign
can u tell me what kind of math operationns?
• June 29th, 2012, 08:37 AM
Norm
Re: How to convert from string to math sign
Quote:

kind of math operations
To do addition use the +: x + y
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;```