Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Convert String into an expression.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74755
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper,

That worked awesome! Thank you so much!
 
Greenhorn
Posts: 12
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic