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: Convert String Literal into Operation

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Convert String Literal into Operation

    After searching on several forums for a while IW as unable to find similar posts, so I was hoping I would be able to get some help. I want to convert a string literal into an operation, however I'm not sure how to start. Here's a (bad) attempt I've made so far


    class test
    {
        public static void main (String args[])
        {
            String eq1 = "x/2+6";
            double eq2 = eq1; // <- want "double eq2 = x/2+6;"
            System.out.println (eq2);
        }
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Convert String Literal into Operation

    Search the net for the Java scripting API. Also take a look at the classes and interfaces in the javax.script package.

    I've never used this stuff, so I can't advise further.

    db

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert String Literal into Operation

    double eq2 = eq1;
    Perhaps a better example would be;
     double eq2 = evaluateToDouble(eq1);

    Now you need to find the class with a method like that.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert String Literal into Operation

    Use python, or some other similar scripting language. The alternative is to write a parser yourself (not simple!). With Jython, you can integrate your Java program with Python.

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Convert String Literal into Operation

    With a bit of Google and help at another forum, I found JEval, which worked great

Similar Threads

  1. Convert object to String
    By innspiron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:48 AM
  2. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 05:01 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. Convert string to int?
    By connex in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2009, 05:06 AM
  5. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM