-2
public class ReaderWriter extends Bank {

private final String FILENAME = "clients.txt";

public void writeToFile() {
    int i = 0;
    boolean repeat = true;
    Formatter output = null;   // Used to write to file

    try {
        output = new Formatter(FILENAME);
        // Open the file

        while ((i <= accounts.length - 1) && (accounts[i] != null)) {
            output.format("%s\n", accounts[i].getAccountHolder());      
            output.format("%d%n", accounts[i].getAccountNumber());
            output.format("%d%n", accounts[i].getAmount());
            i = i + 1;
        }catch (Exception ex) {

        ex.printStackTrace();
    } finally {
        output.close();                 // Make sure to close the resource after usage.
    }
}

这是Bank类:

public class Bank {

public final int MAX_NUMBER_OF_ACCOUNTS = 10;
public int max = 0;

String name1;
int money1;
int number1;

Scanner input = new Scanner(System.in);
BankAccount[] accounts = new BankAccount[MAX_NUMBER_OF_ACCOUNTS];
public void greateAccount() {
    int i = 0;
    boolean repeate2 = true;
    System.out.println("You have chosen to create a new account.");
    System.out.println("Enter the name of the account holder:  ");
    name1 = input.next();

    System.out.println("Enter the account no.");
    number1 = input.nextInt();

    System.out.println("Enter the initiating amount: ");
    money1 = input.nextInt();

    while (repeate2 == true) {
        if (accounts[i] == null) {
            if (i < 1) {
                accounts[i] = new BankAccount();
                accounts[i].setAccountHolder(name1);
                accounts[i].setAccountNumber(number1);
                accounts[i].setAmount(money1);
                repeate2 = false;
            } else {
                if (ifAccountExist(number1) != true) {
                    accounts[i] = new BankAccount();
                    accounts[i].setAccountHolder(name1);
                    accounts[i].setAccountNumber(number1);
                    accounts[i].setAmount(money1);
                    repeate2 = false;
                } else {
                    System.out.println("****This account ALREADY EXIST!****");
                    System.out.println("*************************************");
                    System.out.println();
                    max = max - 1;
                    repeate2 = false;
                }
            }
        }
        i = i + 1;
    }
    max++;
}

现在我想写入文本文件帐号、姓名和金钱。我的代码不起作用。我不写它不能从数组中检索值我不知道为什么?你能帮助我吗?

4

1 回答 1

0

代码无法编译,原因有很多......

例如:

  1. 为什么这条线:

    ex.printStackTrace();

    浮动在您的类 ReaderWriter (第 21 行)中并远离一个 catch 块?

  2. 为什么你没有包含声明?

于 2013-03-12T00:45:16.507 回答