• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input error in math equation

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic