• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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: 80656
477
  • 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.
 
Bartender
Posts: 15741
368
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic