This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Convert String into an expression. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert String into an expression." Watch "Convert String into an expression." New topic
Author

Convert String into an expression.

Patrick Boos
Greenhorn

Joined: Sep 09, 2008
Posts: 15
Hello,

Is there a way to take a String, for instance "4 * 5" from a JTextField and convert it into an expression where I will get a result? Thank you.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

If it's only a simple math expression like what you showed above, it's pretty simple to roll out your own parsing algorithm. But if you deal with a more complex math expression, you are better off using JEP


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Patrick Boos
Greenhorn

Joined: Sep 09, 2008
Posts: 15
Thanks a lot. Do you have any resources that you can point me to so that I can do this conversion?
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Sorry, I just realized that JEP has been commercialized. Besides, the older version of JEP uses GPL license may not be suitable if you're doing any commercial project. Try to Google for any other free Java math expression libraries.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
You will need to get a compiler book, eg Aho Sethi Lam and Ullman. That explains how strings can be parsed, and shows how to convert arithmetic to postfix. There are lots of tools available; you usually use a scanner (eg lex, flex, JFlex) and a parser eg yacc, ANTLR, CUP.

The problem is similar to the first stages in writing a compiler.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

It's not necessary to write your own parser.

Java 6 has a scripting API, which you could use for this, as long as the expression is valid JavaScript (or any other supported scripting language). Example:

If you are not using Java 6, have a look at BeanShell.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Patrick Boos
Greenhorn

Joined: Sep 09, 2008
Posts: 15
Thank you all very much. I will try them out.
Patrick Boos
Greenhorn

Joined: Sep 09, 2008
Posts: 15
Jesper,

That worked awesome! Thank you so much!
Yaki Kumar
Greenhorn

Joined: Feb 01, 2010
Posts: 12

Hi!! The code works perfect.

However, I still have a question (I am new to Java).
What I want to do is simply get operands (with and without fractional part) and operators separated. Code below only gives me operands. For simplicity I am considering +,-,*,/ as the candidate operators. Off course you can give me a direct expression to use, but I would appreciate if you can give an Idea of the approach to build it. I am finally trying to solve expression (infix notation) given as a String. I think, constructing a tree with operator as Non-Leaf Nodes and operand as leaf nodes and then traversing the tree would help me (Not an issue now).

For eg.
input: 1.2*3+45-5/3+2
output: 1.2 * 3 + 45 - 5 / 3 + 2



Thanks.
 
GeeCON Prague 2014
 
subject: Convert String into an expression.