Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic