IntelliJ open source
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
Author

Variable Expressions.

Michael Hanson
Greenhorn

Joined: Jun 14, 2009
Messages: 3

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
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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

This message was edited 1 time. Last update was at by Fred Hamilton

Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8832

Check out the javax.script package. It's the ScriptEngine and its eval() methods you'll mostly be interested in.

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Michael Hanson
Greenhorn

Joined: Jun 14, 2009
Messages: 3

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

Joined: May 13, 2009
Messages: 670

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?
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10017

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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?
pete stein
Ranch Hand

Joined: Feb 23, 2007
Messages: 915

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.
Bear Bibeault
Author and opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36591

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?

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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.

This message was edited 1 time. Last update was at by Fred Hamilton

Bear Bibeault
Author and opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36591

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.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10017

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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
Bartender

Joined: Sep 28, 2004
Messages: 10017

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8832

Exactly.

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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.

This message was edited 2 times. Last update was at by Fred Hamilton

Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8832

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

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Messages: 670

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
Bartender

Joined: Sep 28, 2004
Messages: 10017

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench