Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将数据从对象数组写入文本文件_Java_Arrays - Fatal编程技术网

在Java中将数据从对象数组写入文本文件

在Java中将数据从对象数组写入文本文件,java,arrays,Java,Arrays,现在我想把账号、姓名和钱写进文本文件。 我的代码不起作用。我不写它不能从数组中检索值我不知道为什么? 你能帮我吗?由于许多原因,代码无法编译 例如: 为什么选择这一行: 例如,打印跟踪 是否在类ReaderWriter第21行中浮动并远离一个捕捉块 为什么没有包含声明 我不能编译你的代码..它看起来不像你的代码。首先,什么东西不具体工作,如何工作?我在这里写的错误是:它是编译。问题是,当我尝试在文件中写入时,它在数组中找不到任何内容。它是[0]==null,但事实并非如此。我不明白为什么?如果看

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

由于许多原因,代码无法编译

例如:

为什么选择这一行:

例如,打印跟踪

是否在类ReaderWriter第21行中浮动并远离一个捕捉块

为什么没有包含声明


我不能编译你的代码..它看起来不像你的代码。首先,什么东西不具体工作,如何工作?我在这里写的错误是:它是编译。问题是,当我尝试在文件中写入时,它在数组中找不到任何内容。它是[0]==null,但事实并非如此。我不明白为什么?如果看不到代码的其余部分,很难知道。我可以看到逻辑上有许多不规则之处。还有行名称1=input.next;,number1=input.nextInt;,和money1=input.nextInt;看起来很可疑。在继续之前,请验证number1、money1和name1的值。最后,在ifaccounts[i]==null行,如果i<1,您应该真正修复逻辑,它不会在这些部分中输入至少一次,因为i<1只是一次迭代,您已经将repeate2设置为false;什么意思是while循环只到达一次。整个程序正在运行一个循环。我可以输入多个帐户,然后以数组形式保存。然后我可以在控制台中打印全部或仅打印一个帐户。一切都很好。第一个循环应该输入onse。当我进入第一个帐户时。然后,当我想输入secend帐户时,它进入secont循环。只是说我没有输入相同的帐号。整个程序正在运行。我只需要将写帐户添加到文件中。下次当我启动程序时,我需要从文件中读取帐户。
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.
    }
}
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++;
}