• 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

Scanner is skipping

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a scanner to read in some expression like
(add 3 4) and (mul 2 3). Now I am suppose to be able to read expressions like (add 3(mul 2 3)) and it work. But it keeps on throwing an error. When I debug the program I can tell that the scanner is reading the second paren and then it reads something else in so therfore I get a message saying that about my number formatting say 3mul is not valid. Here is my code it is quite long sorry. Any help would be great.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would usually try to get the code to compile successfully, before trying to debug it. You seem to have omitted a few things here. Additionally, if you could provide a simple main() method that uses the class in a way that demonstrates the error you're talking about, that would be helpful. Or failing that, show us the error you get (with stack trace), and see what exactly is at the line numbers which are referenced in the stack trace. That will probably be more helpful that letting us look at the code from scratch and trying to guess what you're doing.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry about that. I forgot the import statement.



and here is the main method

Here is the errors I get
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of your methods is still missing a return statement. And your stack trace refers to a parseInt() method which does not exist anywhere in the code you've shown. I would recommend editing the original post and replacing all the code at once with the correct code, rather than bringing in bits and pieces that don't quite fit together.

I would guess that, if you've got a Scanner, there's no need for Integer.parseInt() - you can check for a number with hasNextInt(), and get the number with nextInt(). And this has the advantage of just grabbing the numeric part of the input, and stopping at the first non-numeric input (e.g. "mul".) So then you can parse that next non-numeric bit with another method. It looks like you're tryign to use Integer.parseInt() to parse too much at once ("3mul"), and it's choking on the non-numeric part.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay I am sorry for the missing code. I had a lot of commented out code that I deleted before posting it and I must have accidently deleted some of the good code. Here is the code all of it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic