Results 1 to 5 of 5
Thread: Question - Loader Method
- 02-12-2013, 02:50 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Question - Loader Method
Im getting the Exception in thread "main" java.io.FileNotFoundException: Accounts (The system cannot find the file specified)
Im trying to set it so if there is no file then it just says "sorry no file" and goes back to Menu();
inside the loop I set the linklist 'AccountList' to null so any previous info saved with be overwritten so no duplicates. but if the file is not found
it keeps the info in the linklist incase the user wants to continue with what he was doing.
Java Code:public void Loader(LinkList AccountList) throws IOException { File AccountsFile = new File("Accounts"); Scanner keyboard = new Scanner(AccountsFile); if (AccountsFile.exists()) { //make account list empty AccountList.head = null; while(keyboard.hasNextLine()) { Account p = new Account(); String AccountNumberLoaded = keyboard.nextLine(); int AccountInt = Integer.parseInt(AccountNumberLoaded); p.setAccountNumber(AccountInt); p.setName(keyboard.nextLine()); p.setAddress(keyboard.nextLine()); String BalanceLoaded = keyboard.nextLine(); double BalanceDouble = Double.parseDouble(BalanceLoaded); p.setBalance(BalanceDouble); AccountList.add(p); } if (!(keyboard.hasNextLine())) { System.out.println("Nothing to load - File is Empty!"); } } if (!(AccountsFile.exists())) { System.out.println("No File Exists!"); } MainClass.Menu(AccountList); }
- 02-12-2013, 03:12 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Question - Loader Method
If you have processing you want to do should a file not be found then you need to use a try/catch and catch the exception.
You can then handle that processing inside the catch block.Please do not ask for code as refusal often offends.
- 02-12-2013, 03:30 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
- 02-12-2013, 03:50 PM #4
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Re: Question - Loader Method
Ok it now works, however even though the file contains lines it always goes to "nothing to load - file is empty!"
Java Code:public void Loader(LinkList AccountList) throws IOException { //File AccountsFile = new File("Accounts"); //Scanner keyboard = new Scanner(AccountsFile); //if (AccountsFile.exists()) //{ try { File AccountsFile = new File("Accounts"); Scanner keyboard = new Scanner(AccountsFile); while(keyboard.hasNextLine()) { //make account list empty AccountList.head = null; Account p = new Account(); String AccountNumberLoaded = keyboard.nextLine(); int AccountInt = Integer.parseInt(AccountNumberLoaded); p.setAccountNumber(AccountInt); p.setName(keyboard.nextLine()); p.setAddress(keyboard.nextLine()); String BalanceLoaded = keyboard.nextLine(); double BalanceDouble = Double.parseDouble(BalanceLoaded); p.setBalance(BalanceDouble); AccountList.add(p); } if(!(keyboard.hasNextLine())) { System.out.println("Nothing to load - File is Empty!"); } } //if (!(AccountsFile.exists())) catch (Exception e) { System.out.println("No File Exists!"); } MainClass.Menu(AccountList); }
- 02-12-2013, 06:09 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Question - Loader Method
That's because you've asked it to.
The above code occurs in the try block after you have reached the ned of the file, so whenever the code gets to the last line of the file, whether there was anything in the file or not, it prints out that sentence.
That, presumbaly, should be before your the loop.Please do not ask for code as refusal often offends.
Similar Threads
-
Make JVM use My Own Class Loader
By rajyshubeita@gmail.com in forum Advanced JavaReplies: 1Last Post: 11-18-2011, 09:41 PM -
SQL Loader not working
By chinna in forum New To JavaReplies: 5Last Post: 08-18-2011, 10:29 AM -
which class loader cexecutes first
By katturv in forum New To JavaReplies: 1Last Post: 01-10-2011, 06:30 AM -
class loader problems!
By alacn in forum New To JavaReplies: 6Last Post: 08-11-2010, 10:51 PM -
Class loader
By JavaJunkie in forum New To JavaReplies: 1Last Post: 05-06-2009, 01:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
thanks for helping

Bookmarks