• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String into an expression.

 
Patrick Boos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patrick Boos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15444
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Patrick Boos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much. I will try them out.
 
Patrick Boos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper,

That worked awesome! Thank you so much!
 
Yaki Kumar
Greenhorn
Posts: 12
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic