File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Scanner Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner Problems" Watch "Scanner Problems" New topic

Scanner Problems

Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
I am loading a file into a scanner. The file is a text file with a listing of 88,799 names. I am reading the names and writing them out to another text file. The new text file is a list of the names along with random passwords assigned to each name.

Problem is, it stops at line 88,266. The program continues as normal, but it never completed the file. I am using a while(in.hasNext()) loop for processing the file, so the scanner should keep going until there is no more text to process. When it gets to line 88,266 it writes the name to the new file and stops, no password for that name and no more names and passwords at all.

Can anyone think of a reason for a scanner to not continue until there is no more text?

Greg Roberts<br />CIS Student<br />University of West Florida
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm not sure what the cause is. I do know that Scanner objects *hide* Exceptions. Maybe you could check to see if an IOException has been thrown by calling Scanner.ioException(). If no exception has been thrown, a call to this method should return null, however if there was an exception thrown, a call to this method should help you get to the root cause. If you find that no IOException was thrown, it may be helpful if you posted the code for the method that is causing you problems.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
Turns out I had forgotten to call close() on the Scanner and PrintWriters. There was still data in some buffer that had not been written to file at the end of the method, which is why it wasen't finishing the file. When I called close(), it flushed the buffer and finished writing to file.

Its always something small that I forget that causes the biggest problems.
I agree. Here's the link:
subject: Scanner Problems
It's not a secret anymore!