-1

Problem:

I'm trying to figure out how to access the Student Array class in order to create four entries for each Student object, but I'm not sure how to do so, while also allowing the program to create more than just one Student.

public class ClassRoster<T> {

    public static void main(String[]args) {
        ClassRoster<Student> classroster = new ClassRoster<Student>();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Add/Drop/Search?");
        String action = keyboard.nextLine();
        boolean done = false;
        Object temp, c, d, e;
        int fresh, soph, jun, sen;
        Student test = new Student();

        while(!done) {

            if(action.equalsIgnoreCase("Add")) 
                {
                    int counter = 0;
                    System.out.print("Enter Student ID");
                    temp = test.setID(keyboard.nextInt());
                    System.out.println("First name?");
                    c = test.setFirstName(keyboard.nextLine());
                    System.out.println("Last name?");
                    d = test.setLastName(keyboard.nextLine());
                    System.out.println("Academic Level?");
                    e = test.setLevel(keyboard.nextLine());
                    ...
}

And I have another class called Student, where there are four different data entries (ID, FirstName, LastName, Academic Level).

I'm not sure how to access the object which I have created in the correct way. It just gives me an error in this Driver class, and I don't know how to correctly access the array bag.

skomisa
  • 16,436
  • 7
  • 61
  • 102
Ashley Yu
  • 1
  • 3
  • 1
    The listing of your `ClassRoster` class is clearly missing some code. Please update your question to address this. – skomisa Mar 10 '19 at 02:02

1 Answers1

0

but I'm not sure how to do so while also allowing the program to create more than just one Student

Currently you are only creating one specific instance of student with Student test = new Student(); To actually create more than one student, you will have to iterate the whole process of reading all four data entries (ID, FirstName, LastName, Academic Level). Instead of having to initialize the fields (your four data entries) with specific set methods, I would recommend you letting the Student class initialize them with the class constructor. Meaning the Student class should look something like this:

public class Student{
    private final int ID;
    private final String firstname;
    private final String lastname;
    private String level;

public Student(int ID, String firstname, String lastname, String level){
    this.ID = ID;
    this.firstname = firstname;
    this.lastname = lastname;
    this.level = level;
}

ID, firstname and lastname are set to final as you foresee them not to change. However the academic level is ought to change and therefore is not set to final. Now that you have set up a constructor for your Student class, we can get to how to allow the program to insert multiple students at once.

public static void main(String[]args) {
    ClassRoster<Student> classroster = new ClassRoster<Student>();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Add/Drop/Search?");
    String action = keyboard.nextLine();
    boolean done = false;

while(!done) {

    if(action.equalsIgnoreCase("Add")) {

            System.out.print("Enter Student ID");
            int ID = keyboard.nextInt();
            System.out.println("First name?");
            String firstname = keyboard.nextLine();
            System.out.println("Last name?");
            String lastname = keyboard.nextLine();
            System.out.println("Academic Level?");
            String level = keyboard.nextLine();
            Student student = new Student(ID, firstname, lastname, level);

            //we have now created a new instance of Student, now we have to save it in your classroster

            classroster.add(student);
    }
    System.out.println("Next action?");
    action = keyboard.nextLine();
    if(action.equals("done") done = true; //if you write 'done', your loop will finish executing
}

I don't know about your implementation of classroster, but I assume you have implemented it with some kind of list or map, which is why I call the add(Student s) method after creating an instance of Student. To actually then access all students, you will have to implement a method in classroster that returns the saved list of classroster and then iterate through the returned list in the main loop. To actually see what the students look like, you will also have to implement methods for the student instances to for example print out their full names.


I see that you are having a little trouble with arrays, maps and lists as you don't know how to access your students yet. I recommend you reading up on the difference between these three data structure types and simply try to implement them in a small example to see how they work.

pr0f3ss
  • 527
  • 1
  • 4
  • 17