• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

calculate a string expression

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
what's the easiest way to calculate a string expression?
eg.
function("1+1") = 2;
does java provide the function?
thanks
 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jerry Chi,
According to my experience in Java, Java doesn't have method to do that.
Because of that, we have to develop our own method.
Example



Correct me if I am wrong...
Hope this helps

daniel
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are looking for an JavaScript-style eval function in Java - something that doesn't exist. You are not the only one asking for help like this in this forum - which makes me assume this is an assignment. In which case it could be a very difficult assignment - unless you don't have to worry about nested functions and parenthesis? If you don't Fisher Daniel ahs given you part of the answer. The other part is breaking out (or perhaps I should say Tokenizing) the function String and dealing with the constituent parts.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you're in the same class as this person and this person.

So... seeing as though this is a homework problem, and we don't like to DO people's homework for them... what have you tried? what problems have you come up against? How do you think it can work?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerry,
By now you must have understood that there is no eval function in Java as you might have used in Javascript. So you gotta right some code. Now the question is how.

The expression you have is a infix expression
Convert this expression to Postfix (lot of code available on net for this)
Evaluate this postfix expression (lot of code available on net for this too)..
Now you may implement any number of functions that your eval function will support.

I know this forum is for learning.. but if you still need others to do your homework .. we'll help ... but in the long run this will not help..
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On linux, you could use Runtime.exec, to call bc, but of course a call to an external program isn't a good solution, because it's making you platform dependend.

Another approach is, to put the expression in a text, representing java-source as string, and compile that String on the fly.
You need the package tools.jar for that technique, which isn't part of the default classpath.
Probably it will not work on plain JRE-installations - don't know about this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic