0

For my programming class, we have to design a bank account that reads and writes information to a file, which contains information, such as the 10-digit account number, first and last name, middle initial, and balance for people who open accounts. (So it will say that John A. Smith has an account with number 1234567890 and a balance of at least 26.00) Anyway, I'm having trouble programming it to read and write to the file. This is the instructions regarding reading from the file:

"1. When your program begins running, if the file acinfo.dbf exists, it reads data from it and creates objects of the BankAccount class which are then stored in an array list. acinfo.dbf consists of the fields account number, first name, middle initial, and balance. Your program then closes the file. If the file does not exist, your program simply proceeds since this means that no active account exists. All of this is done in the main method."

I've written this so far, but I have very little clue what I'm doing regarding this, and it may be wrong. :(

public class GeauxBank
   {
   public static void main(String[] args)
      {
      ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
      Scanner keyb = new Scanner(System.in);
      String acinfo;
      System.out.print("What is the input file name?");
      acinfo = keyb.next();
      Scanner in = null;

      try
      {
        in = new Scanner(new File(acinfo));
      }
      catch (FileNotFoundException e)
      {
      }

Can anyone help me figure out what to do?

Andrew Kozak
  • 1,631
  • 2
  • 22
  • 35
Cody Rogers
  • 1
  • 1
  • 1
  • You have a lot more to go before we can even help. When you read in from the file you have to create a bank account object, you should only then store them in a list. – JonH Nov 29 '11 at 18:50
  • 1
    Ask your instructor/teaching assistants, that's what they're paid for if you have broad reaching questions. – wkl Nov 29 '11 at 18:55
  • 1
    what exactly do you need help with? reading data from a file? – rana Nov 29 '11 at 21:47

1 Answers1

0

Reading from file:

File file = new File("filename.txt");
FileReader reader;
String line = null;
try {
    reader = new FileReader(file);
    BufferedReader in = new BufferedReader(reader);
    while ((line = in.readLine()) != null)
        System.out.println(line);
    in.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}   

Also you may check: http://docs.oracle.com/javase/tutorial/essential/io/file.html

mol
  • 2,607
  • 4
  • 21
  • 40