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.

Results 1 to 5 of 5

Thread: How to convert from string to math sign

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to convert from string to math sign

    So your're saying it is impossible?

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to convert from string to math sign

    can u tell me what kind of math operationns?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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;
    If you don't understand my answer, don't ignore it, ask a question.

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

    usama8800 (June 29th, 2012)

Similar Threads

  1. Convert string hex to int?
    By Lucky7 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2012, 07:28 AM
  2. [SOLVED] convert int to String
    By itispj in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2011, 10:34 PM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. Conversion of string into integer in Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  5. Convert string to int?
    By connex in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2009, 05:06 AM