• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

RandomAccessFile help

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 14508
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We don't have time for this. We've gotta save the moon! Or check this out:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic