Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings to operators?

 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 7758
61
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
manish paliwal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
BYE

------------------
manish paliwal(or just mann )
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I'll take a look at the things you suggest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic