Results 1 to 5 of 5
  1. #1
    newtojava is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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);
    			}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    newtojava is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Question - Loader Method

    Quote Originally Posted by Tolls View Post
    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.
    How would I go about this, I tried adding a try catch but I cant seem to get it to run past calling the loader thanks for helping

  4. #4
    newtojava is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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);
    			
    	    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Question - Loader Method

    Quote Originally Posted by newtojava View Post
    Ok it now works, however even though the file contains lines it always goes to "nothing to load - file is empty!"

    Java Code:
    	            if(!(keyboard.hasNextLine()))
    	            {
    	            	System.out.println("Nothing to load - File is Empty!");
    	            	
    	            }
    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.

    ** This space for rent **

Similar Threads

  1. Make JVM use My Own Class Loader
    By rajyshubeita@gmail.com in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2011, 09:41 PM
  2. SQL Loader not working
    By chinna in forum New To Java
    Replies: 5
    Last Post: 08-18-2011, 10:29 AM
  3. which class loader cexecutes first
    By katturv in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:30 AM
  4. class loader problems!
    By alacn in forum New To Java
    Replies: 6
    Last Post: 08-11-2010, 10:51 PM
  5. Class loader
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 05-06-2009, 01:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •