This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner Problems

 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic