Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: How to convert from string to math sign

1. ## 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?

2. ## 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.

3. ## Re: How to convert from string to math sign

So your're saying it is impossible?

4. ## Re: How to convert from string to math sign

can u tell me what kind of math operationns?

5. ## Re: How to convert from string to math sign

kind of math operations
To do addition use the +: x + y
to do subtraction use the -: x - y
etc
```if(str.equals("+"))   // test the string
val = x + y;         // do the math operation
else id(str.equals("-"))
val = x - y;```

6. ## The Following User Says Thank You to Norm For This Useful Post:

usama8800 (June 29th, 2012)