【发布时间】:2015-12-12 19:53:36
【问题描述】:
我一直在努力尝试在 java 中理解这个领域,所以我想也许有人在线可以提供帮助。基本上,我需要“准备” 9,000 条空白记录(对于银行),然后选择在特定区域(即帐号、姓名、余额)输入数据。
我知道我必须使用 for 循环,但我不知道它们应该去哪里。目前,我可以创建 9,000 条记录,但在为一条记录输入数据后,它只会重复 9,0000 次,而不是把记录放在一个特定的位置,其余的留空。
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);
}
}
}
编辑:这些是我的作业说明(如果有人好奇或者我没有解释清楚。
Winter Park Bank 将客户记录保存在一个随机访问文件中。编写一个创建 9,000 条空白记录的应用程序,然后允许用户输入客户帐户信息,包括不超过 9999 的帐号、姓氏和余额。将每条新记录插入数据文件中与帐号相同的位置。假设用户不会输入无效的帐号。强制每个名称为八个字符,用空格填充或在必要时截断它。还假设用户不会输入大于 99,000.00 的银行余额。将文件另存为 WinterParkBankFile.java。
【问题讨论】: