Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile help  RSS feed

 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a program and am 95% done, but am stuck. My program creates a file and the user can file it with account ID's and balances. The issue I am having is that when a user enters a duplicate account ID it needs to throw an error and I am thinking after the error it should then ask for more ID's. At this point I can enter all the ID's I want and after I enter a duplicate entry it does throw the error, but then the program exits. How do I get it to continue asking for ID's.....any hints?

[edit]Break long lines and change // comments to /* */ to reduce horizontal scrolling CR[/edit]
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably a bad idea to throw and catch the same Exception in one method. When you throw your Exception, you are transferring control outwith the loop. So the program will terminate.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty simple, move the DuplicateAccountException catch block inside the while loop.

What I would be more concerned about is that you're keeping the file open all the time, and doing your checking directly from the file.
It's probably a better idea to read all the IDs into a set first, and then checking the set whether it already contains the ID. This will be much more efficient than seeking the disk.

Also, consider splitting up your program into separate methods. This will make it easier to read and maintain the program.
 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips...when I load up my work laptop later tonight I will definitly play with the file and try your suggestions...much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!