只需查看我的代码,这为什么不起作用?只是尝试使用Student方法创建新的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;
}
}
构造函数未定义。
答案 0 :(得分:3)
public void Student(String id,String first_name,String last_name,String[] subject_grades) {
这不是构造函数。这是一个称为void
的{{1}}方法。
将其更改为
Student
答案 1 :(得分:-1)
构造函数不期望任何返回类型。 您已将void作为返回类型提供给非法的构造函数。 您可以查看此问题和解答以获取更多信息。 Why do constructors in java not have a return type?