Forums Register Login

Input error in math equation

+Pie Number of slices to send: Send
I am creating a program that will calculate a math equation similar to (5+5) + 6*3 + 18. It works when I put spaces between the integers and operators, however if there is no space it gives me a stack error. I know its because I am using s.nextInt(); for numbers and s.next() for the operators and if there is no space s.next() will pull everything till you get to a space. Any suggestions on how to fix this. Line 57 is my error because of an empty stack. The input is read between 17-24.

Thanks in advance!

+Pie Number of slices to send: Send
Did you read the API on the Scanner class?

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

So it needs the (white)space to know where your tokens are.

You may be able to pass it a regex as a pattern, that would have a whitespace or your operators (with an optional space), but I don't know if you can then get the operator...
get schwifty. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 745 times.
Similar Threads
converting this to a prefix instead of a postfix calculator
A continuation of my previous post, if I may...
String arguments
Trouble evaluating postfix Expression
Another method of parsing a command line string
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:12:57.