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
類:寫入數據從對象數組文本文件的Java
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++;
}
現在我想要寫文本文件的帳號,姓名和金錢。 我的代碼不起作用。我不寫它無法從數組中檢索值我不知道爲什麼? 你能幫我嗎?
我不能編譯你的代碼.. – 2013-03-12 00:26:50
看起來像_your_代碼首先 – 2013-03-12 00:28:27
什麼不明確,以及如何工作的不? – Dan 2013-03-12 00:28:46