Evaluate basic arithmatic in a string

From: David Morris (dmorris_at_dynamicquest.com)
Date: 04/02/04


Date: Fri, 02 Apr 2004 09:31:30 -0500

OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.

Caviots:
myValue can be int, float, long, double, or short.

Likewise, the 5 could also be a variable of any of the above listed
types. ie, "myValue += myValue2".

Just so you know the environment, I an writing a java scripting janguage
where I can have a user build a template on the fly and I interpret all
the java code manually. I am about 75% finished mimicing the JVM in the
  scripting but am lacking basic math functions.

I use a class

class ObjDef {
        Object value;
        Class className;
}

to store and pass values throughout the script interpretation process.

so I know the value and class of myValue and myValue2 and I know to cast
5 to an int if not otherwise set.

The real caviot comes in that if myValue is an int, the className in
ObjDef is int but the Object is Integer.

How do I get myValue ( which is really ObjDef.value ) to an int so I can
do an actully myValue = myValue + 5 and put it back in the ObjDef class?

Realize if I do a hard cast, 25 different possible combinations (5^2),
so I am looking for a different answer.

Thank you for any help you can offer.



Relevant Pages

  • Re: Evaluate basic arithmatic in a string
    ... this is an excellent suggestion to start solving the problem. ... this is for you experienced Java programmers out there. ... >> myValue can be int, float, long, double, or short. ...
    (comp.lang.java)
  • Re: Evaluate basic arithmatic in a string
    ... this is an excellent suggestion to start solving the problem. ... this is for you experienced Java programmers out there. ... >> myValue can be int, float, long, double, or short. ...
    (comp.lang.java.developer)
  • Re: Evaluate basic arithmatic in a string
    ... this is an excellent suggestion to start solving the problem. ... this is for you experienced Java programmers out there. ... >> myValue can be int, float, long, double, or short. ...
    (comp.lang.java.help)
  • Evaluate basic arithmatic in a string
    ... this is for you experienced Java programmers out there. ... myValue can be int, float, long, double, or short. ... ObjDef is int but the Object is Integer. ...
    (comp.lang.java)
  • Evaluate basic arithmatic in a string
    ... this is for you experienced Java programmers out there. ... myValue can be int, float, long, double, or short. ... ObjDef is int but the Object is Integer. ...
    (comp.lang.java.help)