Java 如何初始化多个空白记录,然后向其中添加数据
我一直在努力用java来理解这个领域,所以我想也许有人可以在线帮助我。基本上,我需要为银行输入9000条空白记录,然后选择在特定区域输入数据,即账号、名称、余额 我知道我必须使用for循环,但我不知道它们应该放在哪里。目前,我可以创建9000条记录,但在为一条记录输入数据后,它只复制了90000次,而不是将该记录放在一个特定的位置,而将其余记录留空Java 如何初始化多个空白记录,然后向其中添加数据,java,records,Java,Records,我一直在努力用java来理解这个领域,所以我想也许有人可以在线帮助我。基本上,我需要为银行输入9000条空白记录,然后选择在特定区域输入数据,即账号、名称、余额 我知道我必须使用for循环,但我不知道它们应该放在哪里。目前,我可以创建9000条记录,但在为一条记录输入数据后,它只复制了90000次,而不是将该记录放在一个特定的位置,而将其余记录留空 import java.nio.file.*; import java.io.*; import static java.nio.file.Stan
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class WriteEmployeeFile
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
File myFile = new File ("C:\\Users\\USSI\\Desktop\\CustomerRecords.txt");
Path file = Paths.get("C:\\Users\\USSI\\Desktop\\CustomerRecords.txt");
String s = "000, ,00.00" +
System.getProperty("line.separator");
String delimiter = ",";
int account;
String name;
double balance;
final int QUIT = 999;
final int MAX = 9001;
try
{
OutputStream output = new
BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new
BufferedWriter(new OutputStreamWriter(output));
System.out.print("Enter client account number: ");
account = input.nextInt();
while(account != QUIT)
{
System.out.print("Enter last name for client #" +
account + ": ");
input.nextLine();
name = input.nextLine();
System.out.print("Enter account balance: ");
balance = input.nextDouble();
s = account + delimiter + name + delimiter + balance;
for(int count = 0; count < MAX; ++count)
writer.write(s, 0, s.length());
writer.newLine();
System.out.print("Enter next ID number or " +
QUIT + " to quit: ");
account = input.nextInt();
}
writer.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
编辑:如果有人好奇或者我没有解释清楚,这些是我的作业指导
冬季公园银行将客户记录保存在随机存取文件中。编写一个应用程序,创建9000条空白记录,然后允许用户输入客户帐户信息,包括9999或更少的帐号、姓氏和余额。将每个新记录插入数据文件中与帐号相等的位置。假设用户不会输入无效的帐号。强制每个名称包含八个字符,并在其中填充空格或在必要时截断。还假设用户输入的银行余额不会超过99000.00。将文件另存为WinterParkBankFile.java
不确定您想要什么,但要给出实例化9000个银行帐户的想法,然后按它们在列表中的位置访问它们:
class BankAccount {
int number;
int balance;
String name;
public static void main(String[] args) {
List<BankAccount> accounts = new ArrayList<BankAccount>();
for (int i = 0; i < 9000; i++) {
accounts.add(new BankAccount());
}
// fill these variables from console however you want
int i = 0;
int balanceReadFromConsole = 0;
int numberReadFromConsole = 0;
accounts.get(i).name = "Whatever you want to set as the i-th account's name";
accounts.get(i).balance = balanceReadFromConsole;
accounts.get(i).number = numberReadFromConsole;
}
}
我的任务要求我使用数据流并创建文件,而不是创建数组列表。但我感谢你的意见。