标记构造函数的 ArrayList 属性
Tokenizing an ArrayList attribute of a constructor
所以我有这个 Java Class 具有以下属性以及 setter 和 getter 等:
public class Student implements Comparable<Student> {
//Student attributes
protected String firstName;
protected String lastName;
protected String major;
protected String idNo;
protected ArrayList<String> courseTaken;
protected int credits;
protected double grade;
public Student(){
}
//constructor
public Student(String firstName, String lastName, String major, String idNo, ArrayList<String> courseTaken, int credits, double grade)
{
this.firstName = firstName;
this.lastName = lastName;
this.major = major;
this.idNo = idNo;
this.courseTaken = courseTaken;
this.credits = credits;
this.grade = grade;
}
在我的 Main.java 中,我想读取一个 txt 文件,像这样标记我的学生 class:
List<Student> students = new ArrayList<>();
try
{
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
// read the first line from the text file
String fileRead = br.readLine();
// loop until all lines are read
while (fileRead != null)
{
// use string.split to load a string array with the values from each line of
// the file, using a comma as the delimiter
String[] tokenize = fileRead.split(",");
// assume file is made correctly
// and make temporary variables for the seven types of data
String tempFirstN= tokenize[0];
String tempLastN = tokenize[1];
String tempMajor = tokenize[2];
String tempIdNo = tokenize[3];
String tempCourse = tokenize[4];
int tempCredits = Integer.parseInt(tokenize[5]);
double tempGpa = Double.parseDouble(tokenize[6]);
// create temporary instance of Student object
// and load with three data values
/**this is the problem!!
*
* Student takes in all tokens as Strings when tempCourse is an ArrayList<String>
*
**/
Student tempStudent = new Student(tempFirstN, tempLastN, tempMajor, tempIdNo, tempCourse, tempCredits, tempGpa);
// add to array list
students.add(tempStudent);
编辑:我想读取的文本文件看起来像这样,其中 -999 是 "stop read and go to the next data" 限制器。
Jones,Mary,903452
4342,2.5,A
3311,C
-999
Martin,Joseph,312345
4598,3,C
1122,3
-999
我认为这是可能的。显然不是。我怎样才能做到这一点?
来自代码中的注释:
这就是问题所在!!
当 tempCourse 是 ArrayList<String>
时,学生将所有标记作为字符串接收
tempCourse
是一个字符串,但在构造函数中,您希望 courseTaken 有一个 ArrayList<String>
。显然这是行不通的(没有从单个对象到这些对象的 ArrayLists 的自动转换)。
您要么必须将此字段和构造函数参数设置为字符串(因此每个学生只有一门课程),要么将 tempCourse 标记拆分为单独的字符串(使用另一个附加分隔符,例如分号),填写将它们放入 ArrayList 并将该 ArrayList 传递给构造函数。
您遇到的问题是您的解析代码与文件中的数据根本不匹配。您似乎在尝试读取所有数据,就好像它在一行中一样,然后将其拆分为就好像这一行包含 7 个标记:
String[] tokenize = fileRead.split(",");
String tempFirstN= tokenize[0];
String tempLastN = tokenize[1];
String tempMajor = tokenize[2];
String tempIdNo = tokenize[3];
String tempCourse = tokenize[4];
int tempCredits = Integer.parseInt(tokenize[5]);
double tempGpa = Double.parseDouble(tokenize[6]); // !! 7 tokens !!
但是你的文件根本不是这样构造的:
Jones,Mary,903452
4342,2.5,A
3311,C
-999
Martin,Joseph,312345
4598,3,C
1122,3
-999
相反,文件表示中的每个学生似乎都包含几行,实际上是一个可变数字,第一行仅包含 3 个标记,第二行(可能)包含 3 个,然后任何人都可以猜测下一行是什么显示.
要解决此问题,您必须充分了解文件结构,然后相应地更改解析代码,包括使用内部循环读取文本,直到出现“-999”。
所以我有这个 Java Class 具有以下属性以及 setter 和 getter 等:
public class Student implements Comparable<Student> {
//Student attributes
protected String firstName;
protected String lastName;
protected String major;
protected String idNo;
protected ArrayList<String> courseTaken;
protected int credits;
protected double grade;
public Student(){
}
//constructor
public Student(String firstName, String lastName, String major, String idNo, ArrayList<String> courseTaken, int credits, double grade)
{
this.firstName = firstName;
this.lastName = lastName;
this.major = major;
this.idNo = idNo;
this.courseTaken = courseTaken;
this.credits = credits;
this.grade = grade;
}
在我的 Main.java 中,我想读取一个 txt 文件,像这样标记我的学生 class:
List<Student> students = new ArrayList<>();
try
{
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
// read the first line from the text file
String fileRead = br.readLine();
// loop until all lines are read
while (fileRead != null)
{
// use string.split to load a string array with the values from each line of
// the file, using a comma as the delimiter
String[] tokenize = fileRead.split(",");
// assume file is made correctly
// and make temporary variables for the seven types of data
String tempFirstN= tokenize[0];
String tempLastN = tokenize[1];
String tempMajor = tokenize[2];
String tempIdNo = tokenize[3];
String tempCourse = tokenize[4];
int tempCredits = Integer.parseInt(tokenize[5]);
double tempGpa = Double.parseDouble(tokenize[6]);
// create temporary instance of Student object
// and load with three data values
/**this is the problem!!
*
* Student takes in all tokens as Strings when tempCourse is an ArrayList<String>
*
**/
Student tempStudent = new Student(tempFirstN, tempLastN, tempMajor, tempIdNo, tempCourse, tempCredits, tempGpa);
// add to array list
students.add(tempStudent);
编辑:我想读取的文本文件看起来像这样,其中 -999 是 "stop read and go to the next data" 限制器。
Jones,Mary,903452
4342,2.5,A
3311,C
-999
Martin,Joseph,312345
4598,3,C
1122,3
-999
我认为这是可能的。显然不是。我怎样才能做到这一点?
来自代码中的注释:
这就是问题所在!!
当 tempCourse 是 ArrayList<String>
tempCourse
是一个字符串,但在构造函数中,您希望 courseTaken 有一个 ArrayList<String>
。显然这是行不通的(没有从单个对象到这些对象的 ArrayLists 的自动转换)。
您要么必须将此字段和构造函数参数设置为字符串(因此每个学生只有一门课程),要么将 tempCourse 标记拆分为单独的字符串(使用另一个附加分隔符,例如分号),填写将它们放入 ArrayList 并将该 ArrayList 传递给构造函数。
您遇到的问题是您的解析代码与文件中的数据根本不匹配。您似乎在尝试读取所有数据,就好像它在一行中一样,然后将其拆分为就好像这一行包含 7 个标记:
String[] tokenize = fileRead.split(",");
String tempFirstN= tokenize[0];
String tempLastN = tokenize[1];
String tempMajor = tokenize[2];
String tempIdNo = tokenize[3];
String tempCourse = tokenize[4];
int tempCredits = Integer.parseInt(tokenize[5]);
double tempGpa = Double.parseDouble(tokenize[6]); // !! 7 tokens !!
但是你的文件根本不是这样构造的:
Jones,Mary,903452
4342,2.5,A
3311,C
-999
Martin,Joseph,312345
4598,3,C
1122,3
-999
相反,文件表示中的每个学生似乎都包含几行,实际上是一个可变数字,第一行仅包含 3 个标记,第二行(可能)包含 3 个,然后任何人都可以猜测下一行是什么显示.
要解决此问题,您必须充分了解文件结构,然后相应地更改解析代码,包括使用内部循环读取文本,直到出现“-999”。