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?
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.
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.
If you don't understand my answer, don't ignore it, ask a question.
So your're saying it is impossible?
can u tell me what kind of math operationns?
To do addition use the +: x + ykind of math operations
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;
If you don't understand my answer, don't ignore it, ask a question.
usama8800 (June 29th, 2012)