1

I'm working on a project and have been racking my brain to meet this one requirement. I have two classes Roster and Student. Student contains all of the private variables and Student contains methods to access objects in the Student class.

Code for each below:

    import java.util.ArrayList;

public class Roster {
    Student studObj = new Student(students);
    static String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59","2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87","4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
            "5,Joshua,Selvidge,jselvidge@wgu.edu,26,79,88,91"};
    static ArrayList<Student> studentlist = new ArrayList<Student>();
    

    public static void main(String[] args){
        
        for (int i = 0; i < students.length; i++){
            String[] parts = students[i].split(",");
            int age = Integer.parseInt(parts[4]);
            int grade1 = Integer.parseInt(parts[5]);
            int grade2 = Integer.parseInt(parts[6]);
            int grade3 = Integer.parseInt(parts[7]);
            String studentID = parts[0];
            String firstname = parts[1];
            String lastname = parts[2];
            String email = parts[3];
            add(studentID,firstname,lastname,email,age,grade1,grade2,grade3);
        }
//Part C.1
        print_all();
        print_invalid_emails();
        print_average_grade();
        remove("3");
        remove("3");
        print_all();

    }
//Part B.3.a
    public static void add(String studentID, String firstname, String lastname, String email, int age, int grade1, int grade2, int grade3){
        Student newStudent = new Student(studentID,firstname,lastname,email,age,grade1,grade2,grade3);

        studentlist.add(newStudent);
    }   
//Part B.3.b
    public static void remove(String studentID) {
            int i = Integer.parseInt(studentID);
            if(studentlist.get(i-=1).getStudentID().contains(studentID)){
                studentlist.remove(i);
            return;
            }
            else{
                System.out.println("Student ID " + studentID + " not found!");
            }
//      }       
    }

//Part B.3.c
    public static void print_all(){
        System.out.print(Student.getStudentID());
        System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
        for (Student w : studentlist) {
            w.print();
        }
    }
//Part B.3.d
    public static void print_average_grade(){
        System.out.println("Average grades");
        System.out.println("Student ID\tName\tAverage");
        for (int i = 0; i < students.length; i++){
            String[] parts = students[i].split(",");
            String studentID = parts[0];
            int grade1 = Integer.parseInt(parts[5]);
            int grade2 = Integer.parseInt(parts[6]);
            int grade3 = Integer.parseInt(parts[7]);
            int average = (grade1+grade2+grade3)/3;
            System.out.println(studentID + "\t" + parts[1] + " " + parts[2] + "\t" + average);
        }
    
    }
//Part B.3.e    
    public static void print_invalid_emails() {
        for(String w : students){
            if(!w.contains("@") || !w.contains(".")){
                System.out.println("Email address error in record: Student ID " + w.charAt(0));
            }
        }
    }

Student Class:

import java.util.Arrays;

public class Student {
//B1 - instance variables for each student info
    private String studentID;
    private int[] grades;
    private String firstname;
    private String lastname;
    private String email;
    private int age;
//B2c constructor using all of the input parameters
    public Student(String studentID, String firstname, String lastname, String email, int age, int grade1, int grade2, int grade3){
        super();
        this.studentID = studentID;
        int[] grades = {grade1,grade2,grade3};
        this.grades = grades;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.age = age;

    }
    


    public String toString() {
        return studentID  + "\t\t" + Arrays.toString(grades) + "\t"  + firstname + " " + lastname + "\t" + email + "\t"  + age;
    }

    //B.2.a-b getters and setter methods for each instance variable in B1   
    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public int[] getGrades() {
        return grades;
    }
    public void setGrades(int[] grades) {
        this.grades = grades;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getEmail() {
        
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void print(){
        System.out.println(toString());
    }
        

        

}

}

So the part I'm stuck on is the print_all(); method in the Roster class:

    public static void print_all(){
    System.out.print(Student.getStudentID());
    System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
    for (Student w : studentlist) {
        w.print();
    }
}

using the enhanced for loop works perfectly fine to output all of the student information in a nice tab separated format, but what I am being asked to do is to print all of the data using the print(); method and getters (accessors) from the Student class, which is why I have added the

        System.out.print(Student.getStudentID());

Obviously as it is it does not work. The error I get is

Cannot make a static reference to the non-static method getStudentID(); from the type Student.

I've looked this error up and lots of the answers had to do with accessing objects or methods within the same class, but I have not had success finding out how to access the method from another class. Effectively what I must do is use print(Student.getStudentID();Student.getAge();Student.getGrades();Student.getName()) in order to print everything out. However, using the get methods are only giving me errors. I tried to change the methods in Student class to static but then they were giving errors about returning and being void. Pretty stuck here. Do I need to change my add() method to use setters and then try to access them from the print_all() method? If so, how is the info stored for access by another method to print?

Here is the output as it should look, with the print(); method using accessors from Student{} class:

 Student ID Recent Grades   Name        E-Mail          Age
1       [88, 79, 59]    John Smith  John1989@gmail.com  20
2       [91, 72, 85]    Suzan Erickson  Erickson_1990@gmailcom  19
3       [85, 84, 87]    Jack Napoli The_lawyer99yahoo.com   19
4       [91, 98, 82]    Erin Black  Erin.black@comcast.net  22
5       [79, 88, 91]    Joshua Selvidge jselvidge@wgu.edu   26
Email address error in record: Student ID 2
Email address error in record: Student ID 3
Average grades
Student ID  Name    Average
1   John Smith  75
2   Suzan Erickson  82
3   Jack Napoli 85
4   Erin Black  90
5   Joshua Selvidge 86
Student ID 3 not found!
Student ID  Recent Grades   Name        E-Mail          Age
1       [88, 79, 59]    John Smith  John1989@gmail.com  20
2       [91, 72, 85]    Suzan Erickson  Erickson_1990@gmailcom  19
4       [91, 98, 82]    Erin Black  Erin.black@comcast.net  22
5       [79, 88, 91]    Joshua Selvidge jselvidge@wgu.edu   26
General Grievance
  • 4,555
  • 31
  • 31
  • 45

2 Answers2

0

This is the important error message.

Cannot make a static reference to the non-static method getStudentID(); from the type Student.

You need to call getStudentID() on an instance of the class, and not the class itself. You can try something like this.

public static void print_all() {

    System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
    for (Student w : studentlist) {
        System.out.print(w.getStudentID() + "\t\t");
        System.out.print(w.getGrades() + "\t");
        System.out.print(w.getFirstname()+ " ");
        System.out.print(w.getLastname()+ "\t");
        System.out.print(w.getEmail()+ "\t");
        System.out.print(w.getAge()+ "\t");
        System.out.println(" ");
    }
}

Calling Student.getStudentID() would only work if there was a static (shared) ID for all the students. This is not the case here. You can look at this post for a more complete explanation of the static keyword in java.

Community
  • 1
  • 1
Boo Radley
  • 684
  • 1
  • 13
  • 25
  • It turns out I was using the arraylist studentlist so i needed to do a get().getStudentID() in order to properly call my values. – GrumpyCoder Jun 28 '16 at 17:09
  • @GrumpyCoder You don't *need* to use `get(i).getX()` if you use a `for-each` loop. I updated my answer to provide an alternative to your solution. – Boo Radley Jun 28 '16 at 17:18
  • I like this better than the get and getID I was using, much cleaner. I wanted to hold onto the advanced for loop as well, instead of the counter based loop, I think they're a lot cleaner. – GrumpyCoder Jun 28 '16 at 17:44
0

The error says it all. The method getStudentID() is a non-static method of the Student class. The call Student.getStudentID() is a static call and hence the error. Invoke the method getStudentID() on an instance of a Student.

toro
  • 194
  • 5