Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Stuck on Writing to File

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stuck on Writing to File

    I am Trying to Write my ArrayList to a File Call BankAccount.text,

    I would like to figure out how to get my Text File like

    Name: Danny
    Starting Balance: 1000
    Deposit : 200
    Total Balance : 1200

    Next Person Etc.Etc.



    Here is my Bank.
     import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.ArrayList; 
     
     
    	/** 
    	   This bank contains a collection of bank accounts. 
    	 * @param <BankAccount>
    	*/
    	public class Bank
    	{ 
     
     
     
    	   /**
          Constructs a bank with no bank accounts. 
    	   */
     
    	   public Bank() 
       { 
          accounts = new ArrayList<BankAccount>(); 
       } 
    	   /** 
    	      Adds an account to this bank. 
    	      @param a the account to add 
    	   */
    	   public void addAccount(BankAccount a) 
    	   { 
    	      accounts.add(a); 
    	   } 
     
    	   /** 
    	      Gets the sum of the balances of all accounts in this bank. 
          @return the total balance 
    	   */
    	   public double getTotalBalance() 
    	   { 
    	      double total = 0; 
    	      for (BankAccount a : accounts) 
    	      { 
    	         total = total + a.getBalance(); 
    	      } 
    	      return total; 
    	   } 
     
    	   /** 
    	      Counts the number of bank accounts whose balance is at 
    	      least a given value. 
    	      @param atLeast the balance required to count an account 
    	      @return the number of accounts having at least the given balance 
    	   */
    	   public int count(double atLeast) 
    	   { 
    	      int matches = 0; 
    	      for (BankAccount a : accounts) 
    	      { 
    	         if (a.getBalance() >= atLeast) matches++; // found a match 
    	      } 
    	      return matches; 
    	   } 
     
    	   /** 
    	      Finds a bank account with a given number. 
    	      @param accountNumber the number to find 
    	      @return the account with the given number, or null if there 
    	      is no such account 
    	   */
    	   public BankAccount find(int accountNumber) 
    	   { 
    	      for (BankAccount a : accounts) 
    	      { 
    	         if (a.getAccountNumber() == accountNumber) // found a match 
    	            return a; 
    	      } 
    	      return null; // no match in the entire array list 
    	   } 
     
    	   /** 
    	      Gets the bank account with the largest balance. 
    	      @return the account with the largest balance, or null if the 
    	      bank has no accounts 
    	   */
    	   public BankAccount getMaximum() 
    	   { 
    	      if (accounts.size() == 0) return null; 
    	      BankAccount largestYet = accounts.get(0); 
     
    	      for (int i = 1; i < accounts.size(); i++) 
    	      { 
    	         BankAccount a = accounts.get(i); 
    	         if (a.getBalance() > largestYet.getBalance()) 
    	            largestYet = a; 
    	      } 
    	      return largestYet; 
    	   } 
     
    	   /** 
    	      Adds an account to the bank. 
    	      @param accountNumber the account number of this account 
    	      @param initialBalance the initial balance of this account 
    	   */
    	   public void addAccount(int accountNumber, double initialBalance) 
    	   { 
    		   accounts.add(new BankAccount(accountNumber, initialBalance));
    	   } 
     
    	   public void RemoveAccount(int accountNumber) 
    	   { 
    		   try{
    			   accounts.remove(accountNumber);
    		   }
    		   catch(Exception e)
    		   {
    			   System.err.println("Error result: 0" + e.getMessage());
    		   }
    	   } 
     
     
    	   /** 
    	      Deposits money into an account 
    	      @param accountNumber the account number 
    	      @param amount the amount to be deposited 
    	   */
    	   public void deposit(int accountNumber, double amount) 
    	   { 
    		  find(accountNumber).deposit(amount);
     
    	   } 
     
    	   /** 
    	      Withdraws money from an account. 
    	      @param accountNumber the account number 
    	      @param amount the amount to be withdrawn 
    	   */
    	   public void withdraw(int accountNumber, double amount) 
    	   { 
     
    		find(accountNumber).withdraw(amount);
     
    	   }
    	   /** 
    	      Gets an account balance 
    	      @param accountNumber the account number 
    	      @return the account balance 
    	   */
    	   public double getBalance(int accountNumber) 
    	   {
    		   for (BankAccount b : accounts) 
    		      { 
    		         if (b.getAccountNumber() == accountNumber) // found a match 
     
    		        	 return(b.getBalance());
    		      } 
    		   return (Double) null; 
     
    	   } 
     
    	   public String toString(){
    			return String.format(null, null);
     
    		}
     
     
     
     
    	   public void WritetoFile(ArrayList<BankAccount> accounts) throws IOException
    	   {
    		   for (int i = 0; i < accounts.size(); i++) 
    		   {
    	            write(accounts);
    		   }
    	    }
    	    void write(ArrayList<BankAccount> accounts) throws IOException  {
    	        Writer out = new OutputStreamWriter(new FileOutputStream("bankAccounts.txt"));
    	        try {
    	          out.write("Name:" + \n 
    "Starting Balance:" + \n 
    "Deposit :" + \n 
    "Total Balance :" + \n 
    );
    	        }
    	        finally {
    	          out.close();
    	        }
    	    }
     
     
     
     
    	   private ArrayList<BankAccount> accounts; 
    	}

    And here is my Test Main
    public class BankTester 
    	{ 
    	   public static void main(String[] args) 
    	   { 
    	      Bank bank = new Bank(); 
    	      int dannysAccount = 0; 
    	      int sallysAccount = 1; 
    	      int harrysAccount = 2; 
    	      int jerrysAccount = 3; 
     
    	      int DavidsAccounts = 110;
     
    	      bank.addAccount(dannysAccount, 1000); 
    	      bank.addAccount(sallysAccount, 2000); 
    	      bank.addAccount(harrysAccount, 3000); 
    	      bank.addAccount(jerrysAccount, 10000); 
     
    	      bank.RemoveAccount(DavidsAccounts);
     
    	      bank.deposit(dannysAccount, 200); 
    	      bank.withdraw(sallysAccount, 500); 
    	      bank.deposit(harrysAccount, 1000); 
    	      bank.withdraw(jerrysAccount, 7000); 
     
    	      System.out.println( 
    	         "Danny's Account Balance: " + bank.getBalance(dannysAccount)); 
    	      System.out.println("Expected: 1200"); 
    	      System.out.println( 
    	         "Sally's Account Balance: " + bank.getBalance(sallysAccount)); 
    	      System.out.println("Expected: 1500"); 
    	      System.out.println( 
    	         "Harry's Account Balance: " + bank.getBalance(harrysAccount)); 
    	      System.out.println("Expected: 4000"); 
    	      System.out.println( 
    	         "Jerry's Account Balance: " + bank.getBalance(jerrysAccount)); 
    	      System.out.println("Expected: 3000"); 
    	   } 
    	}


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Stuck on Writing to File

    Sounds like a good idea. Looks nice.

    Where are you stuck on it?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on Writing to File

    I am Stuck on the Write to File, its not Creating a File, as i dont have a Method in Main to Call my WritetoFile()

    and like to get my Starting Balance, WithDraw and deposit Balance into a String and Write that to a File.
    which should give me

    Name: Danny
    Starting Balance: 1000
    Deposit : 200
    Total Balance : 1200


    I have also have done this to a clone of my Project, but on run time i am getting BankAccount.txt (No such file or directory)
    BankAccount.txt not found


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.StringTokenizer;
     
     
    /** 
       This program tests the Bank class. 
    	*/
    	public class BankTester 
    	{ 
     
    	public static void main(String[] args) 
    	   { 
     
     
     
     
    		   try
    		    {
    			BufferedReader inFileStream = 
    			    new BufferedReader(new FileReader("BankAccount.txt"));
    			 Bank bank = new Bank(); 
    			  String name = null;
    		      int dannysAccount = 0; 
    		      int sallysAccount = 1; 
    		      int harrysAccount = 2; 
    		      int jerrysAccount = 3; 
    		      int DavidsAccounts = 110;
     
    		  	  bank.addAccount(dannysAccount, 1000); 
    		      bank.addAccount(sallysAccount, 2000); 
    		      bank.addAccount(harrysAccount, 3000); 
    		      bank.addAccount(jerrysAccount, 10000); 
     
    		      bank.RemoveAccount(DavidsAccounts);
     
    		      bank.deposit(dannysAccount, 200); 
    		      bank.withdraw(sallysAccount, 500); 
    		      bank.deposit(harrysAccount, 1000); 
    		      bank.withdraw(jerrysAccount, 7000); 
    			String line = inFileStream.readLine();
     
     
    			while (line != null)
    			    {
    				StringTokenizer parser = 
    				    new StringTokenizer(line);
     
    				// reading name, age, gpa from the line
    				name = parser.nextToken();
    				dannysAccount = Integer.parseInt(parser.nextToken());
    				//gpa = Integar.parseFloat(parser.nextToken());
     
     
    			      System.out.println( 
    			         "Danny's Account Balance: " + bank.getBalance(dannysAccount)); 
    			      System.out.println("Expected: 1200"); 
    			      System.out.println( 
    			         "Sally's Account Balance: " + bank.getBalance(sallysAccount)); 
    			      System.out.println("Expected: 1500"); 
    			      System.out.println( 
    			         "Harry's Account Balance: " + bank.getBalance(harrysAccount)); 
    			      System.out.println("Expected: 4000"); 
    			      System.out.println( 
    			         "Jerry's Account Balance: " + bank.getBalance(jerrysAccount)); 
    			      System.out.println("Expected: 3000");
    				line = inFileStream.readLine();
    			    }
     
     
    			inFileStream.close();
     
    		    }
    		catch(FileNotFoundException e)
    		    {
    			System.out.println(e.getMessage());
    			System.out.println("BankAccount.txt not found");
    			System.exit(-1);
    		    }
    		catch(IOException e)
    		    {
    			System.out.println(e.getMessage());
    			System.out.println("Error reading BankAccount.txt");
    			System.exit(-1);
    			}
     
     
     
    		try
    		    {
    	        	PrintWriter outFileStream = 
    			    new PrintWriter(new FileOutputStream("BankAccount.txt"));
     
    			outFileStream.println("Hello World");
    			outFileStream.println("Good Morning");
    			outFileStream.close();
    		    }
    		catch(FileNotFoundException e)
    		    {
    			System.out.println(e.getMessage());
    			System.out.println("Can't open BankAccount.txt");
    			System.exit(-1);
    			}
     
    		catch(IOException e)
    		    {
    			System.out.println(e.getMessage());
    			System.out.println("Error writing BankAccount.txt");
    			System.exit(-1);
    			}
     
     
    		File outFile = new File("BankAccount.txt");
    		System.out.println(outFile.exists());
    		System.out.println(outFile.length());
    		System.out.println(outFile.getName());
    		System.out.println(outFile.getPath());
    	    }
     
     
    	   }

Similar Threads

  1. Stuck at Writing in Txtbox
    By blad3cro in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:15 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. new line in file writing
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 30th, 2012, 10:06 AM
  4. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  5. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM