gpt4 book ai didi

java - 对构造函数的 ArrayList 属性进行标记

转载 作者:行者123 更新时间:2023-11-30 02:48:54 25 4
gpt4 key购买 nike

所以我有这个 Java 类,它具有以下属性以及 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 文件,将其标记为我的 Student 类,如下所示:

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 是“停止读取并转到下一个数据”限制器。

Jones,Mary,903452
4342,2.5,A
3311,C
-999
Martin,Joseph,312345
4598,3,C
1122,3
-999

我认为这是可能的。显然不是。我怎样才能做到这一点?

<小时/>

来自代码中的注释:
这就是问题所在!!
当 tempCourse 为 ArrayList<String> 时,Student 将所有标记作为字符串接收

最佳答案

您遇到的问题是您的解析代码与文件中的数据根本不匹配。您似乎试图读取所有数据,就好像它在一行上一样,然后将其拆分,就好像这一行包含 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

相反,文件表示中的每个 Student 都包含几行,实际上数量可变,第一行仅包含 3 个标记,第二行(可能)是 3 个,然后任何人都可以猜测下一行显示的内容。

要解决此问题,您必须完全理解文件结构,然后相应地更改解析代码,包括使用内部循环读取文本,直到出现“-999”。

关于java - 对构造函数的 ArrayList 属性进行标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com