• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable Expressions.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello people and sorry for the simple question.

I wish to store an expression in a string variable like this:



and then somehow be able to evaluate the expression within that variable. Is there some way that i can do this easily. Is there some sort of ParseExpr method or am I just being plane programmerspeak silly.

Thanks in advance for your contributions,
mintsmike
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you probably already understand, one can always parse a string and do just about anything you want with it, but I know of no built in construct that will parse and evaluate such a string for you, I'd say you have to do it yourself.

That being said, I don't know every last detail of java, maybe someone else knows of a way to do it.

ok maybe someone else has already done the work. Try googling java expression parser, you'll get lots of hits
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the javax.script package. It's the ScriptEngine and its eval() methods you'll mostly be interested in.
 
Michael Hanson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK i think i got the correct method, is it this one: here,

And if so does it return and parse what i want it to be able to do.

@Fred Hamilton: Thanks for your advice, I acutally programmed my CAS calculator in C++ with my own self-programmed parser. I know how hard that was, I even has to set up about 20 lines of code to convert a string to an integer. But then I wanted a GUI for it, so I started porting it to java. Now I know that java has the Integer class method Integer.parseInt(java.lang.String str) and also the Float class method Float.parseFloat(java.lang.String str), so they should help fundamentally to cut code size. Thanks for the advice tho,

Regards,
mintsmike
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. I usually devote a good deal of effort to parsing different kinds of strings, so I'm interested in the subject.

I had a look at that eval() method in ScriptEngine, this is not an area of java I am familiar with, so It's not at all clear to me how this helps you. Given that the API doc for this method doesn't say anything about mathematical expressions, I assume you still have a significant amount of coding to do?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:Cool. I usually devote a good deal of effort to parsing different kinds of strings, so I'm interested in the subject.

I had a look at that eval() method in ScriptEngine, this is not an area of java I am familiar with, so It's not at all clear to me how this helps you. Given that the API doc for this method doesn't say anything about mathematical expressions, I assume you still have a significant amount of coding to do?



The scripting engine is for scripting languages. I don't believe mathematical expressions is one of those languages, but it doesn't matter. Most languages support some form of mathematical expressions -- so you just need to slightly modify the expression into something that can be parsed.

For example, if you choose Javascript as your language, you probably don't have to do any modification, as the mathematical expression example in this topic look like Javascript.

Henry
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Fred Hamilton wrote:Cool. I usually devote a good deal of effort to parsing different kinds of strings, so I'm interested in the subject.

I had a look at that eval() method in ScriptEngine, this is not an area of java I am familiar with, so It's not at all clear to me how this helps you. Given that the API doc for this method doesn't say anything about mathematical expressions, I assume you still have a significant amount of coding to do?



The scripting engine is for scripting languages. I don't believe mathematical expressions is one of those languages, but it doesn't matter. Most languages support some form of mathematical expressions -- so you just need to slightly modify the expression into something that can be parsed.

For example, if you choose Javascript as your language, you probably don't have to do any modification, as the mathematical expression example in this topic look like Javascript.

Henry



Thanks for that, your explanation of scripts makes sense, but I don't see how you, or Rob, was able to conclude that any kind of script was involved, based on the original question. Unless you are saying that 5 * 5 +2 looks like javascript?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:Thanks for that, your explanation of scripts makes sense, but I don't see how you, or Rob, was able to conclude that any kind of script was involved, based on the original question. Unless you are saying that 5 * 5 +2 looks like javascript?


The original question didn't involve javascript specifically, but as explained above is a tool that is now part of the Java weaponry that can allow us to parse mathematical String expressions, and so yes, it can help solve the original problem.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:Unless you are saying that 5 * 5 +2 looks like javascript?


It's a perfectly legal JavaScript expression. Why do you think that it is not?
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Fred Hamilton wrote:Unless you are saying that 5 * 5 +2 looks like javascript?


It's a perfectly legal JavaScript expression. Why do you think that it is not?



It's not that I thought it wasn't a legal javascript expression, but it seems to be a perfectly legal expression in java also. So at first I could not understand how Rob was able to deduce that scripts were involved. Pete's answer seems to suggest something I did not know, that there is something inherent in JavaScript that makes the job of parsing mathematical expressions easier. Otherwise, Rob would not have suggested a method in ScriptEngine. I guess.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:... that there is something inherent in JavaScript that makes the job of parsing mathematical expressions easier.


Yes. As Rob pointed out, the eval() method.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:It's not that I thought it wasn't a legal javascript expression, but it seems to be a perfectly legal expression in java also.



Yes, but Java isn't a scripting language. To do the same thing, you need to generate some sort of Java source, then compile it, load the class file, so you can run to get the result. Or as you mention before, you have to write some sort of parser to convert that string into a result.

Fred Hamilton wrote:So at first I could not understand how Rob was able to deduce that scripts were involved. Pete's answer seems to suggest something I did not know, that there is something inherent in JavaScript that makes the job of parsing mathematical expressions easier. Otherwise, Rob would not have suggested a method in ScriptEngine. I guess.



First, if you look at the scripting engine, you'll notice that it takes a string, and evaluates it. So, it will take any valid string as a script and give you the result -- so yea, it's like a mathematical parser, but it's a Javascript parser. And it's built into Java 6, so no need to use an external parser.

But second, interestingly Javascript does have a function that does this -- if you were coding in javascript, and had an expression in a string, you can evaluate the expression (any legal Javascript actually) with the eval() function.

Henry
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Fred Hamilton wrote:... that there is something inherent in JavaScript that makes the job of parsing mathematical expressions easier.


Yes. As Rob pointed out, the eval() method.



ok, if you are saying that eval() is in fact a javascript method, then I didn't pick up that that fact either.

Anyways, thanks to Henry for the last explanation, and to all for taking the time. It's sort of coming together. I'm sure if I sleep on it and review the materials tomorrow, I'll understand it better.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:
ok, if you are saying that eval() is in fact a javascript method, then I didn't pick up that that fact either.

Anyways, thanks to Henry for the last explanation, and to all for taking the time. It's sort of coming together. I'm sure if I sleep on it and review the materials tomorrow, I'll understand it better.




I don't think that Rob was referring to Javascript's eval() function. I think he was refering to the eval() methods which are part of the scripting engine, being called from Java.

Henry
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Exactly.



Fine that's what I thought originally, but along the way I picked up the impression that you meant otherwise. It doesn't matter now, but if you care to look back over the thread, I'm sure you can spot where the misimpression arose in the first place.

Anyways, moving on, since we've gone to this much effort, I may as well try to get it right, so, as far as I can determine,...

The issue is not that is easier to parse an expression using javascript, which is what I thought you guys were talking about, but rather that we are treating the expression as if it were javascript, and the script parsing functionality of eval() lends itself to parsing mathematical expressions also.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it can be confusing that both the ScriptEngine and the target language have an eval() method / function.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Well, it can be confusing that both the ScriptEngine and the target language have an eval() method / function.



true enough, but that wasn't what confused me. Anyways, it looks like I have it right with my last post, so all's well that ends well.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The issue is not that is easier to parse an expression using javascript, which is what I thought you guys were talking about, but rather that we are treating the expression as if it were javascript, and the script parsing functionality of eval() lends itself to parsing mathematical expressions also.



Correct...

One other thing. We seemed to be so focused on JavaScript that there may be readers who think that the scripting engine is for JavaScript... that's not really true.

The scripting engine is for scripting languages, just like JDBC is for databases. If you are a fan of a particular scripting language, there may be a driver for it... It's still a bit early for some scripting languages as this is a Java 6 functionality.

Regardless, we were recommending Javascript because that this the scripting engine that is built into Java 6 -- no need to install an external jar file. (and it can easily handle the expression)

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic