gpt4 book ai didi

java - 尝试创建新对象时,构造函数出现未定义错误

转载 作者:行者123 更新时间:2023-12-01 19:36:40 25 4
gpt4 key购买 nike

看看我的代码,为什么这不起作用?只是尝试使用 Student 方法创建新的学生对象。

public class Student {

public String id, first_name,last_name;
//assuming it was subject grades
public String[] subject_grades;

public void Student(String id, String first_name, String last_name, String[] subject_grades) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.subject_grades = subject_grades;
}

public static boolean readFile(String filename) { File file = new File(filename);
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
List<Student> list = new ArrayList<>();
String[] words = scanner.nextLine().split(",");
new Student(words[0], words[1], words[2],
new String[] {words[3], words[4], words[5], words[6], words[7], words[8]}
));
}
} catch (FileNotFoundException e) {
System.out.println("Failed to read file");
}

return true;

}

}

构造函数未定义。

最佳答案

 public void Student(String id,String first_name,String last_name,String[] subject_grades) {

这不是构造函数。它是一个名为 Studentvoid 方法。

更改为

 public Student(String id,String first_name,String last_name,String[] subject_grades) {

关于java - 尝试创建新对象时,构造函数出现未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57196769/

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