首页 > 解决方案 > 尝试创建新对象时构造函数未定义错误

问题描述

只需查看我的代码,为什么这不起作用?只是尝试使用 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;

    }

}

构造函数未定义。

标签: java

解决方案


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

这不是构造函数。它是一种void称为Student.

将其更改为

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

推荐阅读