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

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

HallOfFameMember对象具有10个字段。按照名字,姓氏,死者,出生月份,出生日期,出生年份,总收入,已玩年份,入职年份,名人堂ID

出生的日期/月份/年份都将单独上课以创建出生日期。然后将出生日期设置在HallOfFameMember中。记录的字段将由不同数量的空格和/或制表符(空白)分隔。

从该文件读取记录后,使用记录中的字段作为构造函数调用中的参数,调用HallOfFameMember的10-arg构造函数。将此新的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(){

    }


}

最佳答案

几个提示:

我不想为您做家庭作业,但是我想不出一个快速的教程来指出您的工作范围。

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

String delims = "\\s+";


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

另外,您需要将拆分移到while循环中,并需要创建每个HallOfFameMember对象。在循环的每个迭代中,您要:


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


如果您想详细了解这些步骤中的任何一个,我很乐于详细说明。

08-17 21:29