主要

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the name of the input file: ");
    String fileName = input.next();

    ClassRoll c = new ClassRoll();
    c.display();

    prompt();
    System.out.print("Enter a command: ");
    String ans = input.next();


    while (!(ans.equalsIgnoreCase("q") || ans.equalsIgnoreCase("quit"))){
        if(!(ans.equalsIgnoreCase("i") || ans.equalsIgnoreCase("insert") ||
             ans.equalsIgnoreCase("a") || ans.equalsIgnoreCase("average") ||
             ans.equalsIgnoreCase("n") || ans.equalsIgnoreCase("names") ||
             ans.equalsIgnoreCase("r") || ans.equalsIgnoreCase("remove") ||
             ans.equalsIgnoreCase("f") || ans.equalsIgnoreCase("find") ||
             ans.equalsIgnoreCase("d") || ans.equalsIgnoreCase("display")))
        {
            System.out.println("Bad Command");
        } else {
            switch (ans.charAt(0)){
                case 'i':   c.insert();
                    break;
                case 'a':   c.sortAverage();
                    c.display();
                    break;
                case 'n':   c.sortLastNames();
                    c.display();
                    break;
                case 'r':   c.delete();
                    c.display();
                    break;
                case 'f':   Student s=c.find();
                    if (s == null) {
                        System.out.println("Student not found");
                    } else {
                        System.out.println(s.toString());
                    }
                    break;
                 case 'd':   c.display();
                    break;
            }
        }
        prompt();
        System.out.print("Enter a command: ");
        ans=input.next();
    }

    c.save();
    System.out.println("Thank you!");
}

public static void prompt(){
    System.out.println("Enter one of the following commands");
    System.out.println("i or insert to insert a student in the class roll");
    System.out.println("a or average to sort the students based on their average");
    System.out.println("n or names to sort the students based on their last names");
    System.out.println("r or remove to remove a student from the class roll");
    System.out.println("f or find to find a student in the class roll");
    System.out.println("d or display to display the class roll");
    System.out.println("q or quit to exit the program");
}


CassRoll()

ArrayList students = new ArrayList();
String title;
String fileName;

public ClassRoll(String f) throws IOException {

    Scanner fileScan, lineScan;
    String line;
    fileName = f;
    fileScan = new Scanner(new File(f));
    title = fileScan.nextLine();
    System.out.println("Title = " + title);

    while (fileScan.hasNext()) {
        line = fileScan.nextLine();
        lineScan = new Scanner(line);
        lineScan.useDelimiter("\t");
        String lastName = lineScan.next();
        String firstName = lineScan.next();
        Student s = new Student(firstName, lastName);
        s.setScore1(lineScan.nextInt());
        s.setScore2(lineScan.nextInt());
        s.setScore3(lineScan.nextInt());
        students.add(s);
        ClassRoll c;
        c = new ClassRoll();
        c.display();
    }
}

void display() {

    DecimalFormat fmt = new DecimalFormat("0.00");
    System.out.println("\t\t\t" + title);

    double classAverage = 0.0;

    for (int i = 0; i < students.size(); i++) {
        Student s = (Student) students.get(i);
        System.out.print(s.toString());
        System.out.println("\t" + fmt.format(s.getAverage()));
        classAverage = classAverage + s.getAverage();
    }

    System.out.println("\t\t\t" + fmt.format(classAverage /
       students.size()));
    }

    public void insert() {

        Scanner input = new Scanner(System.in);
        System.out.print("First Name: ");
        String firstName = input.next();
        System.out.print("Last Name: ");
        String lastName = input.next();
        System.out.print("Score 1: ");
        int score1 = input.nextInt();
        System.out.print("Score 2: ");
        int score2 = input.nextInt();
        System.out.print("Score 3: ");
        int score3 = input.nextInt();
        Student s = new Student(firstName, lastName);
        s.setScore1(score1);
        s.setScore2(score2);
        s.setScore3(score3);
        students.add(s);
    }

    private int search(String f, String l) {
    int i = 0;
    while (i < students.size()) {
        Student s = (Student) students.get(i);
        if (s.equals(f, l)) {
            return i;
        } else {
         i++;
        }
    }
     return -1;
    }

    public Student find() {

        Scanner input = new Scanner(System.in);
        System.out.print("First Name: ");
        String firstName = input.next();
        System.out.print("Last Name: ");
        String lastName = input.next();

        int i = search(firstName, lastName);

        if (i >= 0) {
            return (Student) students.get(i);
        } else {
            return null;
        }
    }

    public void delete() {

        Scanner input = new Scanner(System.in);
        System.out.print("First Name: ");
        String firstName = input.next();
        System.out.print("Last Name: ");
        String lastName = input.next();

        int i = search(firstName, lastName);

        if (i >= 0) {
            students.remove(i);
        } else {
            System.out.println("Student not found");
        }
    }

    public void sortLastNames() {
        for (int i = 0; i < students.size() - 1; i++) {
            for (int j = i + 1; j < students.size(); j++) {
                Student s1 = (Student) students.get(i);
                Student s2 = (Student) students.get(j);
                if (s1.compareTo(s2) > 0) {
                    students.set(i, s2);
                    students.set(j, s1);
                }
            }
        }
    }

    public void sortAverage() {
        for (int i = 0; i < students.size() - 1; i++) {
            for (int j = i + 1; j < students.size(); j++) {
                Student s1 = (Student) students.get(i);
                Student s2 = (Student) students.get(j);
                if (s1.getAverage() < s2.getAverage()) {
                    students.set(i, s2);
                    students.set(j, s1);
                }
            }
        }
    }

public void save() throws IOException {
    PrintWriter out = new PrintWriter(fileName);
    out.println(title);
    for (int i = 0; i < students.size(); i++) {
        Student s = (Student) students.get(i);
        out.println(s.toString());
    }
out.close();
}


问题:

大家好!我对此程序有疑问,似乎无法解决。
这行代码在main和CassRoll()上都引发了编译器错误

ClassRoll c =新的ClassRoll();
c.display();

错误是:
类ClassRoll中的构造函数ClassRoll不能应用于给定类型;
必需:字符串
找到:没有参数
原因:实际参数和形式参数列表的长度不同

最佳答案

ClassRoll中的构造函数定义为需要String参数:

公共ClassRoll(String f)

..但是您尝试创建ClassRoll的实例而不传递所需的String:

ClassRoll c =新的ClassRoll();

您应该能够通过将“ filename”字符串传递给构造函数来修复它,如下所示:

ClassRoll c =新的ClassRoll(文件名);

希望这有道理并能有所帮助。

09-10 02:09