aspose file tools*
The moose likes Java in General and the fly likes Scanner is skipping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Scanner is skipping" Watch "Scanner is skipping" New topic
Author

Scanner is skipping

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Feb 03, 2006
Posts: 106
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner is skipping