File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Parsing a regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parsing a regular expression" Watch "Parsing a regular expression" New topic

Parsing a regular expression

Mail Reddy

Joined: Apr 03, 2006
Posts: 1
Write a program in Java that can parse a mathematical expression
String, calculate the expression, and produce a numeric result.

For example, given the string: "2.5+3*4+6/12-7" produce the result 8.0

Requirements: Parser
* expression can contain Integers or floating-point numbers
* expression can contain Operators for addition, subtraction, division
and multiplication
* parser should throw an Exception when the expression is invalid

Requirements: Solution
* Implement the solution in Java
* Compile and test the solution
* The solution should demonstrate good modularity, be self-contained,
reusable and extensible.
* The solution should be clearly written, easy to read, documented, and

Requirements: quality
* This is a timed exercise and should be completed quickly, however you
should take sufficient time to produce a quality result
* A quality solution is simple, concise, complete, well documented,
readable, adaptable, reusable, testable and robust.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Have you tried it?
Harald Kirsch
Ranch Hand

Joined: Oct 14, 2005
Posts: 37
What does this have to do with regular expressions?

Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
This exact message was also posted over at the JDC Forums (or SDN Forums - whatever they're calling it now), also by a brand new user. Nobody there can figure out what the OP wants either.
ak pillai
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
If I could understand it correctly, the String


should be parsed into individual operands and operator.

float operand1 = 2.5
char operator1 = '+'
int operand2 = 3;
char operator2 ='*'

and so on. You need to the execute the above expression which gives you the result of 8.

2.5 + (3*4) + (6/12) - 7 = 8

java j2ee job interview questions with answers | Learn the core concepts and the key areas
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I think it's clear what the poster wants: someone else to do his homework for him. I guess it hasn't occurred to him that the problem is more involved than something that can be provided in a short message. Or that this isn't a place where people do other people's homework.
I agree. Here's the link:
subject: Parsing a regular expression
It's not a secret anymore!