• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mathematical expression parsing  RSS feed

 
Krik Monz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I came across a wierd situation. I need todevelop a method in which we pass a string mathematical expression, and the output needs to be a float or double with the value of the expression.
Eg: The input string to a java method might be 1*2+5*(1+4). The output should be 27 in double format and the input obviously is in string format.


Does any body have come across this same scenario and developed a code for the same. If you guys have please share the same with me. I am badly in need for a method like this.

Thanks in advance.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would use BeanShell.

Include the BeanShell jar in classpath and voila, there is nothing simpler:



This returns 27.0 in my console window.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krish Mohan
I came across a wierd situation. I need todevelop a method in which we pass a string mathematical expression, and the output needs to be a float or double with the value of the expression.


What do you mean by string mathematical expression? Only one operator can be applied to string objects in java and that is +.

Eg: The input string to a java method might be 1*2+5*(1+4). The output should be 27 in double format and the input obviously is in string format


1*2+5*(1+4) is not a String at all. The value of this expression is some int value.

Well if you want to call a method which returns double and you are passing some string value to it like "454". then its possible. Use parseFloat(string) or parseDouble(string) to get the primitive from it.

Read its javadoc...




Naseem
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
What do you mean by string mathematical expression?


I thought he was pretty clear, and the first reply is a good answer -- did you read it? The poster wants to take a String like "2 + 2" (such as you'd find in Java source code) and turn it into the number "4".
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my mistake ... actually expression was not put in double quotes so I thought its an int expression. I agree with you Friedman. Its a really good question. I will try it by BeanShell as posted by Edwin. Can we do the same thing by simple java API without refering any other package.

Thanks Friedman

Naseem
[ July 24, 2006: Message edited by: Naseem Khan ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
Can we do the same thing by simple java API without refering any other package.


Sure, but it would take some work, of course. There's nothing built-in to do it -- you'd have to parse the string yourself.
 
Krik Monz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Edwin and everybody else for the quick reply.
But if I am using a very simple regular expression, which contains only +,-,*,/ and (), does any body have a code with a method in it which have all the parsing logic inbuilt in it and does not have to include any jar's in the class path.
If any body have that, then please do help me out. My boss says he cannot include any more jar's.

Any way thanks a lot to all.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see!

I think I get it.

Well, take a look at this interesting pseucode article:
Algorithm to Compute an Arithmetic Expression in Infix Notation

Also, take a look at this article with Java Code:
Parsing and Evaluating Arithmetic Expressions

That might help!
[ July 25, 2006: Message edited by: Edwin Dalorzo ]
 
Krik Monz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..
So nobody has a reusable method for it right which can be used anywhere.
Any ways thanks a lot...
 
Ravi Gupta
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look dear, i had a similar problem when i had to build a project on the same topic. What i am about to tell you applies to specifically the kind of function you are loooking for, i.e., without including any jar or any other files. Acually, the standard way in which this problem is solved is through the use of two algorithms. The first one is to convert an infix expression to a postfix expression, and the second one is for evaluating a postfix expression. Now, u can find these algorithms in any book on data structures, they will most probably be given in the section on application of stacks. Well, to implement those algorithms in C, C++ or Java is fairly simple. The problem occurs when you use expressions that involve multidigit numbers like 67, 89 and so on. the algorithms are designed to work on single digits only.
The complete process is this : get the expression in a string, then scan that string character by character for blanks. Copy the entire string to another string minus the blanks. Now u can apply the algorithms once u figre out how to get past the problem ofmulti digits. A hint for you : u have to use an array of strings.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"java.ravi gupta",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!