This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I am working on a project to create a calculator, and to accept input, I am using the Scanner class to record the input given as a String. I then want to parse that String into an int. However, I don't know how to parse it properly. Here is my code.
This is the error message I get.
-DEBUG: The String calculation has just been created.
-DEBUG: The Scanner has just been created.
-DEBUG: The boolean calculateFinished has just been created.
-DEBUG: The input has just been recorded.
Exception in thread "main" java.lang.NumberFormatException: For input string: "12+6"
"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
If you are using Scanner, it has methods which give you an int directly. You would find it very difficult to parse a String like “12+6”, however. “12+6”, with spaces required, would be easier. Look here and here.
The reason I don't want to use Scanner to scan ints directly is that if I add a graphical interface on later, I want the display panel to display an equation like this: 1+2+3/4, and follow the order of operations. If I do that with an int, it'll calculate too fast. I'm not sure if my reasoning is solid, cause I'm sleepy, but yeah.
Joined: Oct 13, 2005
You can pass a String to a Scanner: new Scanner("12 + 6"); But if you want to parse it, you must have something, eg whitespace, to delimit the numbers from the operators.
Alix Ollivier wrote:I am working on a project to create a calculator...However, I don't know how to parse it properly...
Hmmm. Can I suggest that this might be the basic problem?
And if that sounds obvious, then why are you writing code?
You cannot solve a problem by coding; you can only solve it by thinking...and reading...and scribbling...and thinking some more.
And only when you've done all that and know what you want to do, then you open your Java editor...
Believe me, things will become an awful lot simpler when you get into that habit.
However, just to set you on your way:
1. If this expression involves exactly TWO numbers and an operator in between, then forget the rest of this list.
2. The way we puny humans write mathematical expression is called "infix" notation and computers hate it, because it's horribly illogical.
3. The way computers like to deal with mathematical expressions is with something called Reverse Polish Notation (RPN).
4. A very clever Dutchman called Edsker Dijkstra invented the Shunting yard algorithm to deal with this precise problem: converting puny human expressions to RPN.
If you're only interested in point 1, then you need to look at String.split() and see how you might break up your simple expression.
If you're interested in general parsing of infix expressions (points 2-4), I suspect you're not quite ready for #4; and that's unfortunately where it ends (for the moment; but hopefully only for the moment). If you want to prove me wrong, by all means have a crack at it, and I'll try and help you if you do. But it seems a bit of a stretch at the moment to me.
Isn't it funny how there's always time and money enough to do it WRONG?
Nope, absolutely no monkey wrench; just good sense.
@Alix: Henry's post brings up a very good point (which I completely forgot about): Don't re-invent the wheel.
It has to be said that using a scripting engine is likely to be a lot slower than a pure Java-based solution, but hey, if it's fast enough, that's all you need.