作业帮助;从多个文件读入向量/数组
Posted
技术标签:
【中文标题】作业帮助;从多个文件读入向量/数组【英文标题】:Homework Help; Reading from multiple files into Vectors/Arrays 【发布时间】:2011-12-08 12:45:03 【问题描述】:好的,我的任务是从 2 个单独的文件中读取。第一个文件读入“HallOfFameMember”,然后应该存储在一个向量中。
第二个应该读入,然后存储到 HallOfFame 数组中
HallOfFameMember 对象有 10 个字段。依次为名字、姓氏、已故、出生月份、出生日期、出生年份、总收入、播放年数、入会年份、名人堂 id
出生的日/月/年都去一个单独的类来创建一个出生日期。然后在 HallOfFameMember 中设置出生日期。记录的字段将由不同数量的空格和/或制表符(空格)分隔。
从该文件中读取一条记录后,调用 HallOfFameMember 的 10 参数构造函数,使用记录中的字段作为构造函数调用中的参数。将此新的 HallOfFameMember 对象添加到 tempHallOfFameMemberVec。
HallOfFame 对象有 5 个字段。依次为 hallOfFameId、城市、costToVisit、numberOfVisitorsPerYear 和名称。
从该文件中读取一条记录后,调用类 HallOfFame 的 5 参数构造函数,使用从文件中的行获得的参数。将这个新的 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()
【问题讨论】:
【参考方案1】:几个提示:
我不想为你做作业,但我想不出一个快速教程来指出你正在做什么。
您在 .split 的正确轨道上,但您的分隔符表达式不适用于多个空格/制表符。试试这个:
String delims = "\\s+";
这会将您的字符串分解为一个或多个空格字符的任何连续序列。
此外,您需要将拆分移动到您的 while 循环中,以及创建每个 HallOfFameMember 对象。在您想要的循环的每次迭代中:
-
拆分从文件中读取的行以创建一个字符串数组,表示一条记录的值。
使用字符串数组中的值创建一个新的 HallOfFameMember。 (tempHallOfFameArr[0] 代表名字,tempHallOfFameArr[1] 代表姓氏等)
将您创建的新 HallOfFameMember 添加到您的向量中
如果您想详细了解这些步骤中的任何一个,我很乐意详细说明。
【讨论】:
我想评论一下,\\s+ 是一个非常方便的正则表达式,在你的编程生涯的其余部分中要记住以上是关于作业帮助;从多个文件读入向量/数组的主要内容,如果未能解决你的问题,请参考以下文章