【问题标题】:How to prime multiple blank records and then add data to them如何初始化多个空白记录,然后向它们添加数据
【发布时间】: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 Ba​​nk 将客户记录保存在一个随机访问文件中。编写一个创建 9,000 条空白记录的应用程序,然后允许用户输入客户帐户信息,包括不超过 9999 的帐号、姓氏和余额。将每条新记录插入数据文件中与帐号相同的位置。假设用户不会输入无效的帐号。强制每个名称为八个字符,用空格填充或在必要时截断它。还假设用户不会输入大于 99,000.00 的银行余额。将文件另存为 WinterParkBankFile.java。

【问题讨论】:

    标签: java records


    【解决方案1】:

    不知道你想要什么,但给出实例化 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;
    
        }
    }
    

    【讨论】:

    • 我的作业要求我使用数据流并创建一个文件,而不是创建一个数组列表。但我感谢您的意见。
    猜你喜欢
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多