473,408 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Evaluate basic arithmatic in a string

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.

Jul 17 '05 #1
8 7763
> I have a string that contains the following value:

"myValue += 5"

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


Consider using http://www.mozilla.org/rhino/ - the eval() function does this
nicely.

Powerful stuff!
Jul 17 '05 #2
Order types by restriction level:

type: double float long int short
level: 0 1 2 3 4

and cast operand with have higher level to type of lover level,
then perform operation using two operands with same types.
David Morris wrote:
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.


Jul 17 '05 #3
This is an excellent package. However, I am writing a commercial
application and do not want to use 3rd party code in it.

Thanx anyway.
John Harlow wrote:
I have a string that contains the following value:

"myValue += 5"

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

Consider using http://www.mozilla.org/rhino/ - the eval() function does this
nicely.

Powerful stuff!


Jul 17 '05 #4
Ok, this is an excellent suggestion to start solving the problem.

I have coded your suggestion, sort of.

To do this, correct me if I am wrong, I will still need 5 conditional
tests to hardwire the coding for each restriction level one I decide
which one to use.

I can almost accept that to simple do the casting. However, then I need
to do the evaluation. In the case I gave you is "+=". It could be any
arithmetic function ( a + b / d * c ).

Can I then run a common method to do the evaluation whatever the type
is? I can parse through and set a, b, c, and d to the same type based
on the functionality you have already provided. But how can I
generically to math against the values regardless of their type?

Thanks again for the help.
Mykola Rabchevskiy wrote:
Order types by restriction level:

type: double float long int short
level: 0 1 2 3 4

and cast operand with have higher level to type of lover level,
then perform operation using two operands with same types.
David Morris wrote:
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.


Jul 17 '05 #5
David Morris wrote:
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.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.

Jul 17 '05 #6
Yoyoma_2 <Yoyoma_2@[at-]Hotmail.com> wrote in message news:<FGgbc.12539$oR5.7778@pd7tw3no>...
David Morris wrote:
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.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.


JavaCC
https://javacc.dev.java.net/
Jul 17 '05 #7
Also, JFlex and CUP (http://www.cs.princeton.edu/~appel/modern/java/CUP/)

Mike
HG******@nifty.ne.jp (hiwa) wrote in message news:<68*************************@posting.google.c om>...
Yoyoma_2 <Yoyoma_2@[at-]Hotmail.com> wrote in message news:<FGgbc.12539$oR5.7778@pd7tw3no>...
David Morris wrote:
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.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.


JavaCC
https://javacc.dev.java.net/

Jul 17 '05 #8
On 12 Apr 2004 19:33:57 -0700, no*****************@scovetta.com
(Michael Scovetta) wrote or quoted :
> > I need, in Java, to be able to evaluate this expression.

see http://mindprod.com/jgloss/eval.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave B. | last post by:
Hey gang, I am probably missing something basic. I have a bunch of arrays in one of my ASP scripts, and I need to construct the reference to the variable through some string contatenations. ...
1
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return...
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
15
by: Phlip | last post by:
Javascripters: I have an outer page and an inner iframe. The outer page calculates some javascript, and wants the inner frame to run it. The inner frame should hit a page on the same (private)...
2
by: sapnakiran | last post by:
Can u tell me how can we build up arithmatic operation without using arithmatic operator
6
by: Vince | last post by:
Hello all, I am using Visual Basic to open a saved query and then save information in the query to an array for later use. The problem is that the same query shows different results when opened...
2
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text)...
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and...
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.