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 Strings to operators? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings to operators?" Watch "Strings to operators?" New topic

Strings to operators?

Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Just for fun, I've decided to make a simple calculator as an exercize. And this got me to wondering the following.
In making a calculator, one needs two strings to hold the input, then convert these strings to integers (I'm making a very simple calculator). But is there a way to take a number-style string containing a sign for an arithmetical operation, say, "+", and evaluate the whole thing? E.g., can one construct a mathematical function expression from "7+5"? I've perused the libraries, to no avail, but the libraries are, well, big, so maybe I've missed something.
If not, I guess I'll just have to find out which operator button has been pushed and use the appropriate operation. But it would be nice to do something like the above.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

I'm not aware of anything in the standard libraries that do what you want. Making a calculator can be challenging and educational. Do some research on Reverse Polish Notation, infix and postfix notation, and stacks. Good luck.

Junilu - [How to Ask Questions] [How to Answer Questions]
manish paliwal

Joined: May 18, 2001
Posts: 16
You should first learn about Wrapper class. After reading this class i am sure u might be able to solve operation on String(internally integers)

manish paliwal(or just mann )

manish paliwal(or just mann )
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Thanks guys, I'll take a look at the things you suggest.
I agree. Here's the link:
subject: Strings to operators?
It's not a secret anymore!