Ok, so I have to write a program for school that will read data from a text file, split the data (comma delimited) into the correct type, pass that information on to a class, store the class object into an array.

This is what I have so far:

import java.io.*;


public class BankTest{

public static void main(String [] args)throws IOException{
    
    /* ********************************************************
     * 
     * This was the first exercise to send data to the 
     * BankAccount class.
     * 
    BankAccount ba1 = new BankAccount();
    BankAccount ba2 = new BankAccount(135687, "chris servin",
                                     100.0,false);
    System.out.println(ba1.getOwner());
    System.out.println(ba1.getExperiation());
    System.out.println(ba2.getOwner());
    System.out.println(ba2.getExperiation());
    System.out.println(ba2.getBalance());
    ba2.deposit(200.0);
    System.out.println (ba2.getStatus());
    System.out.println(ba2.getBalance());
    ba2.withdrawl(100.0);
    System.out.println(ba2.getBalance());
    System.out.println(ba2.toString());
    ***********************************************************
    */
  
  int size;
  String str;
  String db[];
  
  BufferedReader input =  new BufferedReader(new FileReader("database.txt"));
  
  str = input.readLine();
  size = Integer.parseInt(str);
  
  db = new String[size];
  
  str = input.readLine();
  
  //System.out.println(db);
  
  for (int i = 0; i < size; i++){
    
  String[] info = str.split(",");
  
  //BankAccount ba(i) = new BankAccount();
  
  BankAccount ba = new BankAccount(int info[0], String info[1], 
                          double [2], boolean [3]);  
      
  //System.out.println(info);
    

  
  
  str = input.readLine();
  }
  
  }
}

Now I don't want too much help writing this, but I am stuck, I have 2 errors that keep coming up that I just can't figure out how to solve.

The first is if I use the statement

BankAccount ba(i) = new BankAccount();

I get - Error: D:\EPCC AA Computer Science\Spring Semester 2011\Java 2\Programs\BankTest.java:49: ';' expected

The second error I get if I remove the (i) variable (which I wanted to create individual instances of the ba object is:

Error: D:\EPCC AA Computer Science\Spring Semester 2011\Java 2\Programs\BankTest.java:51: '.class' expected

I am getting this error on the line where I am attempting to pass the elements in the info array created in the line.split statement.

Can someone please help, I'm at a loss as to how to accomplish what I am trying to get done.

Thanks

You can't create a new array instance like that.
First you create a new array of BankAccounts, giving it a size. This will have all its elements initially null.
Then you can store a BankAccount into any of the array elements:

BankAccount[] accounts = new BankAccount[10]; 
// create array of 10 BankAccount references, all null

accounts[4] = new BankAccount(...  
// store ref to a new BankAccount in array element 4

After some discussion with my instructor, he pointed me in the right direction and I've come up with:

import java.io.*;


public class BankTest{

public static void main(String [] args)throws IOException{
    
    /* ********************************************************
     * 
     * This was the first exercise to send data to the 
     * BankAccount class.
     * 
    BankAccount ba1 = new BankAccount();
    BankAccount ba2 = new BankAccount(135687, "chris servin",
                                     100.0,false);
    System.out.println(ba1.getOwner());
    System.out.println(ba1.getExperiation());
    System.out.println(ba2.getOwner());
    System.out.println(ba2.getExperiation());
    System.out.println(ba2.getBalance());
    ba2.deposit(200.0);
    System.out.println (ba2.getStatus());
    System.out.println(ba2.getBalance());
    ba2.withdrawl(100.0);
    System.out.println(ba2.getBalance());
    System.out.println(ba2.toString());
    ***********************************************************
    */
  
  int size;
  String str;
  
  BufferedReader input =  new BufferedReader(new FileReader("database.txt"));
  
  str = input.readLine();
  size = Integer.parseInt(str);
  
  String[] db = new String[size];
  
  str = input.readLine();
  
  while (str != null){
    
    String[] info = str.split(",");
    
    int num;
    double money;
    boolean status;
 
    num = Integer.parseInt(info[0]);
    money = Double.parseDouble(info[2]);
    status = Boolean.parseBoolean(info[3]);
    
    BankAccount ba = new BankAccount(num, info[1], 
                                   money, status);
  
    System.out.println(ba);
    
    

  
  
    str = input.readLine();
  }
  
  }
}

Now I just need to figure out how to store each instance of ba into the db array.

Edited 5 Years Ago by Harliquin: code not displaying correctly

Ah, let me see if I can't correct that based on the information you've given me, thank you

By golly, I think I got it. It's working exactly as intended. Took me a few minutes to figure out how to store the instances of each object into the array once I corrected how I initialized the array (Thank's for your help James) but this is what I have now, and it appears to be working whenever I print out a specific element.

import java.io.*;


public class BankTest{

public static void main(String [] args)throws IOException{
    
    /* ********************************************************
     * 
     * This was the first exercise to send data to the 
     * BankAccount class.
     * 
    BankAccount ba1 = new BankAccount();
    BankAccount ba2 = new BankAccount(135687, "chris servin",
                                     100.0,false);
    System.out.println(ba1.getOwner());
    System.out.println(ba1.getExperiation());
    System.out.println(ba2.getOwner());
    System.out.println(ba2.getExperiation());
    System.out.println(ba2.getBalance());
    ba2.deposit(200.0);
    System.out.println (ba2.getStatus());
    System.out.println(ba2.getBalance());
    ba2.withdrawl(100.0);
    System.out.println(ba2.getBalance());
    System.out.println(ba2.toString());
    ***********************************************************
    */
  
  int size;
  String str;
  
  BufferedReader input =  new BufferedReader(new FileReader("database.txt"));
  
  str = input.readLine();
  size = Integer.parseInt(str);
  
  BankAccount[] db = new BankAccount[size];
  
  str = input.readLine();
  int i = 0;
  while (str != null){
    
    String[] info = str.split(",");
    
    int num;
    double money;
    boolean status;
 
    num = Integer.parseInt(info[0]);
    money = Double.parseDouble(info[2]);
    status = Boolean.parseBoolean(info[3]);
    
    BankAccount ba = new BankAccount(num, info[1], 
                                   money, status);
  
    db[i] = ba;
    
    i++;
  
    str = input.readLine();
  }
  
     System.out.println(db[9]);
  
  }
}

Excellent!
Suggest you mark this thread "solved" - if you have any more problems you should start a new thread.
J

ps - better version of line 64

for (int i=0; i < db.length; i++) System.out.println(db);

Excellent!
Suggest you mark this thread "solved" - if you have any more problems you should start a new thread.
J

ps - better version of line 64

for (int i=0; i < db.length; i++) System.out.println(db);

The origional line 64 was just my tester to make sure it was working correctly. I modified the line within the while loop to print out all of the results.

db[i] = ba;
     System.out.println(db[i]);
    i++;

this is my print line for the final product.

This question has already been answered. Start a new discussion instead.