家庭作业帮助;从多个文件读取到向量/数组中

发布于 2024-12-10 06:57:06 字数 7876 浏览 0 评论 0原文

好吧,我的任务是从两个单独的文件中读入。第一个文件读取“HallOfFameMember”,然后应存储在向量中。

第二个应该读入,然后存储到 HallOfFame 数组中

。 HallOfFameMember 对象有 10 个字段。按照firstName、lastName、死者、出生月份、出生日期、出生年份、totalEarned、yearsPlayed、yearInducted、名人堂id的顺序,

出生的日/月/年都将进入一个单独的类来创建出生日期。然后,出生日期将在 HallOfFameMember 中设置。记录的字段将由不同数量的空格和/或制表符(空白)分隔。

从此文件中读取记录后,调用 HallOfFameMember 的 10 个参数构造函数,使用记录中的字段作为构造函数调用中的参数。将此新的 HallOfFameMember 对象添加到 tempHallOfFameMemberVec。

HallOfFame 对象有 5 个字段。按 HallOfFameId、城市、costToVisit、numberOfVisitorsPerYear 和名称的顺序排列。

从该文件读取记录后,使用从文件中的行获取的参数调用 HallOfFame 类的 5-arg 构造函数。将此新的 HallOfFame 对象添加到 tempHallOfFameArr。

这一切都可以从命令行完成。

我知道我当前的代码行不通,我只是想找出一些方法来做到这一点。向量和 BufferedReader 对我来说都是全新的,我一直在尝试使用 javapractice.com 以及其他一些网站上的示例作为参考。我知道这将是我忽略/遗漏的一些小事情,当我弄清楚它时,我会感到很沮丧。

无论如何,我当前的问题是这样的。

如何从以“任意数量的空格/制表符”作为分隔符的文件中读入。然后将其解析到适当类中的适当字段中?

给我代码是没有帮助的,如果你能给我指向一个网站或链接,我可以阅读它来获得我的废话时刻,那就太好了。

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;

public class HW4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        FileReader inputFileA = new FileReader("");
        FileReader inputFileB = new FileReader("");


        BufferedReader inputB = new BufferedReader(inputFileB);

        Vector<HallOfFameMember> tempHallOfFameMemberVec = new Vector<HallOfFameMember>();


        try{
            BufferedReader inputA = new BufferedReader(inputFileA);
            try {
                String line = null;

                while ((line = inputA.readLine()) != null){

                }
            }

        }

        String newHallOfFameLine = inputB.toString();
        String delims = "[ \t]";
        HallOfFame[] tempHallOfFameArr = newHallOfFameLine.split(delims);

        for (int i = 0; i < args.length; i += 5) {
            tempHallOfFameArr[i/5].setHallOfFameId(Integer.parseInt(args[i]));
            tempHallOfFameArr[i/5].setCity(args[i+1]);
            tempHallOfFameArr[i/5].setCostToVisit(Integer.parseInt(args[i+2]));
            tempHallOfFameArr[i/5].setNumberOfVisitorsPerYear(Integer.parseInt(args[i+3]));
            tempHallOfFameArr[i/5].setName(args[i+4]);

        }




        }

class HallOfFameMember {

    private String firstName;
    private String lastName;
    private boolean deceased;
    private int dateOfBirth;
    private double totalEarned;
    private int yearsPlayed;
    private int yearInducted;
    private int HallOfFameId;

    public HallOfFameMember() {
    }

    public HallOfFameMember(String firstName, String lastName,
            boolean deceased, int day, int month, int year, double totalEarned,
            int yearsPlayed, int yearInducted, int hallOfFameId) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.deceased = deceased;
        this.dateOfBirth = month + day + year;
        this.totalEarned = totalEarned;
        this.yearsPlayed = yearsPlayed;
        this.yearInducted = yearInducted;
        HallOfFameId = hallOfFameId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean isDeceased() {
        return deceased;
    }

    public void setDeceased(boolean deceased) {
        this.deceased = deceased;
    }

    public int getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(int dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public double getTotalEarned() {
        return totalEarned;
    }

    public void setTotalEarned(double totalEarned) {
        this.totalEarned = totalEarned;
    }

    public int getYearsPlayed() {
        return yearsPlayed;
    }

    public void setYearsPlayed(int yearsPlayed) {
        this.yearsPlayed = yearsPlayed;
    }

    public int getYearInducted() {
        return yearInducted;
    }

    public void setYearInducted(int yearInducted) {
        this.yearInducted = yearInducted;
    }

    public int getHallOfFameId() {
        return HallOfFameId;
    }

    public void setHallOfFameId(int hallOfFameId) {
        HallOfFameId = hallOfFameId;
    }

    public double averageYearlySalary(double averageYearlySalary) {
        return averageYearlySalary = (totalEarned / yearsPlayed);
    }
}

class Date {

    private int month;
    private int day;
    private int year;

    public Date(int month, int day, int year) {
        super();
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

}

class HallOfFame {
    private int hallOfFameId;// ID of the hall of fame
    private String name;// Name of the hall of fame
    private String city;// the city in which the hall of fame is located
    private double costToVisit;// cost in dollars for a visitor to a hall of
                                // fame for 1 day
    private int numberOfVisitorsPerYear;
    private static final double maxCostToVisit = 37.50;

    public HallOfFame() {

    }

    public HallOfFame(int hallOfFameId, String name, String city,
            double costToVisit, int numberOfVisitorsPerYear) {
        super();
        this.hallOfFameId = hallOfFameId;
        this.name = name;
        this.city = city;
        this.costToVisit = costToVisit;
        this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
    }

    public int getHallOfFameId() {
        return hallOfFameId;
    }

    public void setHallOfFameId(int hallOfFameId) {
        this.hallOfFameId = hallOfFameId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public double getCostToVisit() {
        if (costToVisit <= maxCostToVisit) {
            return costToVisit;
        } else
            return maxCostToVisit;
    }

    public void setCostToVisit(double costToVisit) {
        this.costToVisit = costToVisit;
    }

    public int getNumberOfVisitorsPerYear() {
        return numberOfVisitorsPerYear;
    }

    public void setNumberOfVisitorsPerYear(int numberOfVisitorsPerYear) {
        this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
    }

    public static double getMaxcosttovisit() {
        return maxCostToVisit;
    }

    public double totalAnnualRevenue(double totalAnnualRevenue) {
        totalAnnualRevenue += costToVisit * numberOfVisitorsPerYear;
        return totalAnnualRevenue;

    }

}


class ReportWriter{

    private Vector<HallOfFameMember> hallOfFameMemberVec;
    private HallOfFame[] hallOfFameArr;

    public ReportWriter() {

    }

    public Vector<HallOfFameMember> getHallOfFameMemberVec() {
        return hallOfFameMemberVec;
    }

    public void setHallOfFameMemberVec(Vector<HallOfFameMember> hallOfFameMemberVec) {
        this.hallOfFameMemberVec = hallOfFameMemberVec;
    }

    public HallOfFame[] getHallOfFameArr() {
        return hallOfFameArr;
    }

    public void setHallOfFameArr(HallOfFame[] hallOfFameArr) {
        this.hallOfFameArr = hallOfFameArr;
    }

    public void displayReports(){

    }


}

Okay my assignment is to read in from 2 separate files. The first file reads in "HallOfFameMember" and should then be stored in a vector.

The second should read in and then be stored to an array of HallOfFame

The HallOfFameMember object has 10 fields. In order firstName, lastName, deceased, month born, day born, year born, totalEarned, yearsPlayed, yearInducted, hall of fame id

The day/month/year born are all going to a separate class to create a date born. The date born is then set within HallOfFameMember. The Fields for a record will be separated by varying numbers of spaces and/or tabs (whitespaces).

After reading a record from this file, call the 10-arg constructor of HallOfFameMember, using the fields from the record as arguments in the constructor call. Add this new HallOfFameMember object to tempHallOfFameMemberVec.

The HallOfFame object has 5 fields. In order hallOfFameId, city, costToVisit, numberOfVisitorsPerYear, and name.

After reading a record from this file, call the 5-arg constructor of class HallOfFame, using the arguments obtained from the line in the file. Add this new HallOfFame object to tempHallOfFameArr.

This is all to be done from the command line.

I know that my current code will not work, I was just trying to figure out some way to do this. Vectors are completely new to me, along with BufferedReader, and I've been trying to use the examples on javapractice.com as well as a few other sites for reference. I know it will be something small that I am overlooking/missing and Ill have a duh moment when I figure it out.

At any rate my current question is this.

How do I read in from a file that has "any number of white spaces/tabs" as the delimiter. And then parse that into the appropriate fields within the appropriate class?

Giving me the code isn't gonna help, if you could just point me to a website or link that I can read to have my duh moment that would be great.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;

public class HW4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        FileReader inputFileA = new FileReader("");
        FileReader inputFileB = new FileReader("");


        BufferedReader inputB = new BufferedReader(inputFileB);

        Vector<HallOfFameMember> tempHallOfFameMemberVec = new Vector<HallOfFameMember>();


        try{
            BufferedReader inputA = new BufferedReader(inputFileA);
            try {
                String line = null;

                while ((line = inputA.readLine()) != null){

                }
            }

        }

        String newHallOfFameLine = inputB.toString();
        String delims = "[ \t]";
        HallOfFame[] tempHallOfFameArr = newHallOfFameLine.split(delims);

        for (int i = 0; i < args.length; i += 5) {
            tempHallOfFameArr[i/5].setHallOfFameId(Integer.parseInt(args[i]));
            tempHallOfFameArr[i/5].setCity(args[i+1]);
            tempHallOfFameArr[i/5].setCostToVisit(Integer.parseInt(args[i+2]));
            tempHallOfFameArr[i/5].setNumberOfVisitorsPerYear(Integer.parseInt(args[i+3]));
            tempHallOfFameArr[i/5].setName(args[i+4]);

        }




        }

class HallOfFameMember {

    private String firstName;
    private String lastName;
    private boolean deceased;
    private int dateOfBirth;
    private double totalEarned;
    private int yearsPlayed;
    private int yearInducted;
    private int HallOfFameId;

    public HallOfFameMember() {
    }

    public HallOfFameMember(String firstName, String lastName,
            boolean deceased, int day, int month, int year, double totalEarned,
            int yearsPlayed, int yearInducted, int hallOfFameId) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.deceased = deceased;
        this.dateOfBirth = month + day + year;
        this.totalEarned = totalEarned;
        this.yearsPlayed = yearsPlayed;
        this.yearInducted = yearInducted;
        HallOfFameId = hallOfFameId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean isDeceased() {
        return deceased;
    }

    public void setDeceased(boolean deceased) {
        this.deceased = deceased;
    }

    public int getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(int dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public double getTotalEarned() {
        return totalEarned;
    }

    public void setTotalEarned(double totalEarned) {
        this.totalEarned = totalEarned;
    }

    public int getYearsPlayed() {
        return yearsPlayed;
    }

    public void setYearsPlayed(int yearsPlayed) {
        this.yearsPlayed = yearsPlayed;
    }

    public int getYearInducted() {
        return yearInducted;
    }

    public void setYearInducted(int yearInducted) {
        this.yearInducted = yearInducted;
    }

    public int getHallOfFameId() {
        return HallOfFameId;
    }

    public void setHallOfFameId(int hallOfFameId) {
        HallOfFameId = hallOfFameId;
    }

    public double averageYearlySalary(double averageYearlySalary) {
        return averageYearlySalary = (totalEarned / yearsPlayed);
    }
}

class Date {

    private int month;
    private int day;
    private int year;

    public Date(int month, int day, int year) {
        super();
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

}

class HallOfFame {
    private int hallOfFameId;// ID of the hall of fame
    private String name;// Name of the hall of fame
    private String city;// the city in which the hall of fame is located
    private double costToVisit;// cost in dollars for a visitor to a hall of
                                // fame for 1 day
    private int numberOfVisitorsPerYear;
    private static final double maxCostToVisit = 37.50;

    public HallOfFame() {

    }

    public HallOfFame(int hallOfFameId, String name, String city,
            double costToVisit, int numberOfVisitorsPerYear) {
        super();
        this.hallOfFameId = hallOfFameId;
        this.name = name;
        this.city = city;
        this.costToVisit = costToVisit;
        this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
    }

    public int getHallOfFameId() {
        return hallOfFameId;
    }

    public void setHallOfFameId(int hallOfFameId) {
        this.hallOfFameId = hallOfFameId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public double getCostToVisit() {
        if (costToVisit <= maxCostToVisit) {
            return costToVisit;
        } else
            return maxCostToVisit;
    }

    public void setCostToVisit(double costToVisit) {
        this.costToVisit = costToVisit;
    }

    public int getNumberOfVisitorsPerYear() {
        return numberOfVisitorsPerYear;
    }

    public void setNumberOfVisitorsPerYear(int numberOfVisitorsPerYear) {
        this.numberOfVisitorsPerYear = numberOfVisitorsPerYear;
    }

    public static double getMaxcosttovisit() {
        return maxCostToVisit;
    }

    public double totalAnnualRevenue(double totalAnnualRevenue) {
        totalAnnualRevenue += costToVisit * numberOfVisitorsPerYear;
        return totalAnnualRevenue;

    }

}


class ReportWriter{

    private Vector<HallOfFameMember> hallOfFameMemberVec;
    private HallOfFame[] hallOfFameArr;

    public ReportWriter() {

    }

    public Vector<HallOfFameMember> getHallOfFameMemberVec() {
        return hallOfFameMemberVec;
    }

    public void setHallOfFameMemberVec(Vector<HallOfFameMember> hallOfFameMemberVec) {
        this.hallOfFameMemberVec = hallOfFameMemberVec;
    }

    public HallOfFame[] getHallOfFameArr() {
        return hallOfFameArr;
    }

    public void setHallOfFameArr(HallOfFame[] hallOfFameArr) {
        this.hallOfFameArr = hallOfFameArr;
    }

    public void displayReports(){

    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-12-17 06:57:06

一些提示:

我不想为您做作业,但我想不出一个快速教程来指导您完全涵盖您正在做的事情。

您使用 .split 的方式是正确的,但您的分隔符表达式不适用于多个空格/制表符。试试这个:

String delims = "\\s+";

这将在一个或多个空白字符的任何连续序列上分解您的字符串。

此外,您需要将拆分移到 while 循环中,以及创建每个 HallOfFameMember 对象。在循环的每次迭代中,您希望:

  1. 拆分从文件中读取的行以创建表示一条记录的值的字符串数组。
  2. 使用字符串数组中的值创建一个新的 HallOfFameMember。 (tempHallOfFameArr[0] 表示名字,tempHallOfFameArr[1] 表示姓氏,等等。)
  3. 将您创建的新 HallOfFameMember 添加到向量中

如果您想了解有关这些步骤的更多详细信息,我很乐意为您详细说明。

A couple tips:

I don't want to do your homework for you, but I can't think of a quick tutorial to point you to that covers exactly what you're doing.

You're on the right track with .split, but your delimiter expression won't work for multiple spaces/tabs. Try this:

String delims = "\\s+";

That will break up your string on any consecutive sequence of one or more whitespace characters.

Also, you need to move the split up into your while loop, as well as the creation of each HallOfFameMember object. In each iteration of the loop you want to:

  1. Split the line that you read from the file to create an array of strings representing the values for one record.
  2. Create a new HallOfFameMember using the values from your string array. (tempHallOfFameArr[0] for first name, tempHallOfFameArr[1] for last name, etc.)
  3. Add the new HallOfFameMember that you created to your vector

If you'd like more detail on any of these steps, I'm happy to elaborate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文