简体   繁体   中英

Why can I not add an object to my arrayList?

The error message is:

StudentArray.java:14: error: constructor Student in class Student cannot be applied to given types;
      roster.add(new Student(name, id));
                 ^
  required: no arguments
  found: String,String
  reason: actual and formal argument lists differ in length

It repeats this several times and also gives the Cannot Find Symbol error for my numberOfStudents variable. Here is the program, it's unfinished, but I can't move on without figuring this out. Thanks for your help!


import java.util.ArrayList;
import java.util.Scanner;

public class StudentArray {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      ArrayList<Student> roster = new ArrayList<>();
      int numberOfStudents = 0;

   }

   /** Add student method, includes student name and ID */
   public void addStudent(String name, String id) {
      roster.add(new Student(name, id));
      numberOfStudents++;
   }

   /** Overloaded addStudent method to include grades */
   public void addStudent(String name, String id, int grade1, int grade2, int grade3) {
      roster.add(new Student(name, id, grade1, grade2, grade3));
      numberOfStudents++;

   }

   /** Method to enter test grades */
   public void enterTestGrades(int testNumber) {
      int data = -1;
      for (int index : roster) {
         System.out.println("Enter the grade for " + roster.get(index) + ":");
         data = input.nextInt();
         roster.set(index, data);

      }
   }

   public void displayStudentInformation() {
   }

   public void displayMenu() {
   }
}

Student class that the previous program is making an arraylist of, this one compiles fine.

/** Student Class */
public class Student2 {
   private String studentName;
   private String studentId;
   private final int NUMBER_OF_GRADES = 3;
   private int[] studentGrades = new int[NUMBER_OF_GRADES];
   private int averageGrade;
   private final int DEFAULT_GRADE = -1;

   /** No arg constructor */
   public Student2() {
      for (int index = 0; index < NUMBER_OF_GRADES; index++) {
         this.studentGrades[index] = DEFAULT_GRADE;
      }
   }

   /** Constructor that sets Student name and ID */
   public Student2(String name, String id) {
      this();
      this.studentName = name;
      this.studentId = id;
   }

   /** Constructor that sets all Student data */
   public Student2(String name, String id, int grade1, int grade2, int grade3) {
      this(name, id);
      this.studentGrades[0] = grade1;
      this.studentGrades[1] = grade2;
      this.studentGrades[2] = grade3;
   }

   /** Accessor for studentName */
   public String getStudentName() {
      return this.studentName;
   }

   /** Accessor for studentId */
   public String getStudentID() {
      return this.studentId;
   }

   /** Accessor for student grades */
   public int getStudentGrade(int index) {
      return this.studentGrades[index];
   }

   /** Return average student grade */
   public int getAverageStudentGrade() {
      int temp = 0;
      for (int index = 0; index < NUMBER_OF_GRADES; index++) { 
         temp += getStudentGrade(index);
      }

      int averageStudentGrade = temp / NUMBER_OF_GRADES;

      return averageStudentGrade;
   }

   /** Return letter grade */
   public char getLetterGrade(int grade) {

      int temp = grade / 10;
      char letterGrade;
      switch (temp) {
         case 10: 
         case 9: letterGrade = 'A';
                 break;
         case 8: letterGrade = 'B';
                 break;
         case 7: letterGrade = 'C';
                 break;
         case 6: letterGrade = 'D';
                 break;
         default: letterGrade = 'F';
                 break;
      }

      return letterGrade;
   }


   /** Setter method for studentName */
   public void setStudentName(String name) {
      this.studentName = name;
   }

   /** Setter method for studentId */
   public void setStudentId(String id) {
      this.studentId = id;
   }

   /** Setter for studentGrade */
   public void setStudentGrade(int grade, int index) {
      this.studentGrades[index] = grade;
   }
}

Thanks so much if you can help me. I'm totally stuck and this is my final project for my first java class.

The error message is actually pretty explicit: it fails to create an instance of Student because it cannot find any constructor that takes two Strings as arguments.

This means your class Student (not Student2 ) doesn't have this constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM