I'm having trouble with this code. I'm sure it's something stupid (I'm in a class for Java) so please forgive this Java Newbie.

package Student;
import Entity.*;
import ExceptionManagement.StudentException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class DataManagerBean implements DataManager {

    ArrayList<Student> students = new ArrayList<Student>();
    ArrayList<Course> courses = new ArrayList<Course>();
    ArrayList<HCC> school = new ArrayList<HCC>();

    public static void main(String[] args) throws StudentException {
        Student myStudent = new Student();
        compileStudentInfo(myStudent);
        //NewJPanel addSPanel = new NewJPanel();

    }

    public void compileStudentInfo(Student c) throws StudentException {
        boolean cont = true;
        while (cont) {
            // Create the myStudent object and add data to it.
            Student myStudent = new Student();
            // Input Student Record Details
            String name = JOptionPane.showInputDialog("Name:  ");
            myStudent.setName(name);
            String major = JOptionPane.showInputDialog("Major:  ");
            myStudent.setMajor(major);
            int age = Integer.parseInt(JOptionPane.showInputDialog("Age:  "));
            myStudent.setAge(age);
            double gpa = Double.parseDouble(JOptionPane.showInputDialog("GPA:  "));
            myStudent.setGpa(gpa);
            String ssn = JOptionPane.showInputDialog("Social Security Number:  ");
            myStudent.setSsn(ssn);
            addStudent(myStudent);

            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
            if (confirm > 0) {
                cont = false;
            }

        }
    }

    public void addStudent(Student student) throws StudentException {
        String ssn1 = student.getSsn();
        addGrade(ssn1);
        students.add(student);
    }

    public void addGrade(String ssn) throws StudentException {
        boolean cont = true;
        while (cont) {
            // Input Course Information
            String course = JOptionPane.showInputDialog("Course Name:  ");
            String grade = JOptionPane.showInputDialog("Grade:  ");
            String status = JOptionPane.showInputDialog("Status:  ");
            Double gradeDouble = Double.parseDouble(grade);
            Course myCourse = new Course();
            myCourse.setCourse(course);
            myCourse.setGrade(gradeDouble);
            myCourse.setStatus(status);
            courses.add(myCourse);


            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to add more courses to this student?");
            if (confirm > 0) {
                cont = false;
            }
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void deleteStudent(String ssn) throws StudentException {
        for (Student c : students) {
            if (c.getSsn().equals(ssn)) {
                students.remove(c);
            }
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateStudentProfile(Student student) throws StudentException {
        String name = JOptionPane.showInputDialog("Name:  ");
        student.setName(name);
        String major = JOptionPane.showInputDialog("Major:  ");
        student.setMajor(major);
        int age = Integer.parseInt(JOptionPane.showInputDialog("Age:  "));
        student.setAge(age);
        double gpa = Double.parseDouble(JOptionPane.showInputDialog("GPA:  "));
        student.setGpa(gpa);
        String ssn = JOptionPane.showInputDialog("Social Security Number:  ");
        student.setSsn(ssn);

        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Student findStudent(String ssn) throws StudentException {
        for (Student c : students) {
            if (c.getSsn().equals(ssn)) {
                JOptionPane.showMessageDialog(null, "I found a " + c.getName() + " one");
            }
        }
        return null;
    }

    public List<Student> findStudentsByGradesAndMajor(double grade, String major) throws StudentException {
        for (Student c : students) {
            if (c.getMajor().equals(major) && c.getGpa() == grade) {
                JOptionPane.showMessageDialog(null, "I found a " + c.getName() + " one");
            }
        }
        return null;
    }
}

The problem is in the main method, where I'm trying to call the method compileStudentInfo which gathers a students information, creates a student object and then passes the object to the addStudent method which grabs the SSN of the student and calls the addGrade method. The addGrade method then creates an arraylist of courses for the student and returns that arraylist back to the addStudent method which adds the students courses arraylist to the students object and adds that student then to an arraylist of student objects. I know it sounds obscenely complicated and it is but it's the assignment that I have to do. (I'm sure it has to do more with us demonstrating concepts than anything else but I don't have a choice...I could have done this so much better with a mySQL database. LOL)

Anyways on the line in the main method that reads...

compileStudentInfo(myStudent);

... I am getting an error that reads "non-static method compileStudentInfo(Entity.Student) cannot be referenced from a static context."

How can I fix this. I mean I need to be able to call a method that prompts the user for the information and creates the object. I am stuck with the method headers provided by the instructor. ANY advice would be greatly appreciated.

The student object is specified with this code if that helps.

package Entity;
import java.util.ArrayList;
public class Student {
    private String name;
    private int age;
    private String major;
    private double gpa;
    private ArrayList<Course> courses;
    private String ssn;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public ArrayList<Course> getCourses() {
        return courses;
    }

    public void setCourses(ArrayList<Course> courses) {
        this.courses = courses;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
}

Recommended Answers

All 2 Replies

I'm having trouble with this code. I'm sure it's something stupid (I'm in a class for Java) so please forgive this Java Newbie.

package Student;
import Entity.*;
import ExceptionManagement.StudentException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class DataManagerBean implements DataManager {

    ArrayList<Student> students = new ArrayList<Student>();
    ArrayList<Course> courses = new ArrayList<Course>();
    ArrayList<HCC> school = new ArrayList<HCC>();

    public static void main(String[] args) throws StudentException {
        Student myStudent = new Student();
        compileStudentInfo(myStudent);
        //NewJPanel addSPanel = new NewJPanel();

    }

    public void compileStudentInfo(Student c) throws StudentException {
        boolean cont = true;
        while (cont) {
            // Create the myStudent object and add data to it.
            Student myStudent = new Student();
            // Input Student Record Details
            String name = JOptionPane.showInputDialog("Name:  ");
            myStudent.setName(name);
            String major = JOptionPane.showInputDialog("Major:  ");
            myStudent.setMajor(major);
            int age = Integer.parseInt(JOptionPane.showInputDialog("Age:  "));
            myStudent.setAge(age);
            double gpa = Double.parseDouble(JOptionPane.showInputDialog("GPA:  "));
            myStudent.setGpa(gpa);
            String ssn = JOptionPane.showInputDialog("Social Security Number:  ");
            myStudent.setSsn(ssn);
            addStudent(myStudent);

            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
            if (confirm > 0) {
                cont = false;
            }

        }
    }

    public void addStudent(Student student) throws StudentException {
        String ssn1 = student.getSsn();
        addGrade(ssn1);
        students.add(student);
    }

    public void addGrade(String ssn) throws StudentException {
        boolean cont = true;
        while (cont) {
            // Input Course Information
            String course = JOptionPane.showInputDialog("Course Name:  ");
            String grade = JOptionPane.showInputDialog("Grade:  ");
            String status = JOptionPane.showInputDialog("Status:  ");
            Double gradeDouble = Double.parseDouble(grade);
            Course myCourse = new Course();
            myCourse.setCourse(course);
            myCourse.setGrade(gradeDouble);
            myCourse.setStatus(status);
            courses.add(myCourse);


            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to add more courses to this student?");
            if (confirm > 0) {
                cont = false;
            }
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void deleteStudent(String ssn) throws StudentException {
        for (Student c : students) {
            if (c.getSsn().equals(ssn)) {
                students.remove(c);
            }
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateStudentProfile(Student student) throws StudentException {
        String name = JOptionPane.showInputDialog("Name:  ");
        student.setName(name);
        String major = JOptionPane.showInputDialog("Major:  ");
        student.setMajor(major);
        int age = Integer.parseInt(JOptionPane.showInputDialog("Age:  "));
        student.setAge(age);
        double gpa = Double.parseDouble(JOptionPane.showInputDialog("GPA:  "));
        student.setGpa(gpa);
        String ssn = JOptionPane.showInputDialog("Social Security Number:  ");
        student.setSsn(ssn);

        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Student findStudent(String ssn) throws StudentException {
        for (Student c : students) {
            if (c.getSsn().equals(ssn)) {
                JOptionPane.showMessageDialog(null, "I found a " + c.getName() + " one");
            }
        }
        return null;
    }

    public List<Student> findStudentsByGradesAndMajor(double grade, String major) throws StudentException {
        for (Student c : students) {
            if (c.getMajor().equals(major) && c.getGpa() == grade) {
                JOptionPane.showMessageDialog(null, "I found a " + c.getName() + " one");
            }
        }
        return null;
    }
}

The problem is in the main method, where I'm trying to call the method compileStudentInfo which gathers a students information, creates a student object and then passes the object to the addStudent method which grabs the SSN of the student and calls the addGrade method. The addGrade method then creates an arraylist of courses for the student and returns that arraylist back to the addStudent method which adds the students courses arraylist to the students object and adds that student then to an arraylist of student objects. I know it sounds obscenely complicated and it is but it's the assignment that I have to do. (I'm sure it has to do more with us demonstrating concepts than anything else but I don't have a choice...I could have done this so much better with a mySQL database. LOL)

Anyways on the line in the main method that reads...

compileStudentInfo(myStudent);

... I am getting an error that reads "non-static method compileStudentInfo(Entity.Student) cannot be referenced from a static context."

How can I fix this. I mean I need to be able to call a method that prompts the user for the information and creates the object. I am stuck with the method headers provided by the instructor. ANY advice would be greatly appreciated.

The student object is specified with this code if that helps.

package Entity;
import java.util.ArrayList;
public class Student {
    private String name;
    private int age;
    private String major;
    private double gpa;
    private ArrayList<Course> courses;
    private String ssn;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public ArrayList<Course> getCourses() {
        return courses;
    }

    public void setCourses(ArrayList<Course> courses) {
        this.courses = courses;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
}

The easiest way would be to declare the method as static. That would fix the problem :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.