user3117238 user3117238 - 5 months ago 74
Java Question

Creating object using user input to store in Java array

My assignment is the following:

"Create a program that keeps track of specific information for Students. The information stored should be the following:
First Name, Last Name, Major, GPA, UIN, NetID, Age, Gender,
For this simple program we will only need to store 10 students in an array. Your students should be stored in an object called Student.
You should be able to add, display and remove Students in the array.
You will be submitting 2 files for grading: Lab3Driver.java and Student.java"

I'm currently stuck with how to create the student object with all of the separate attributes by using the users input.

My code so far:

public class StudentData{
public static void main(String[] args){
//Creating an info class for the student object
StudentData studentA = new StudentData((strFirstName, strLastName, strMajor,
intGPA, intUIN, strNetID, intAge, strGender));‚Äč

String strFirstName;
String strLastName;
String strMajor;
int intGPA;
int intUIN;
String strNetID;
String strAge;
String strGender;

StudentData(){
// Capturing user input into class attributes
Scanner input = new Scanner(System.in);
System.out.println("Enter First Name");
strFirstName = input.nextLine();

System.out.println("Enter Last Name");
strLastName = input.nextLine();

System.out.println("Enter Major");
strMajor = input.nextLine();

System.out.println("Enter GPA");
intGPA = input.nextLine();

System.out.println("UIN");
intUIN = input.nextLine();

System.out.println("Enter netID");
strNetID = input.nextLine();

System.out.println("Enter Age");
strAge = input.nextLine();

System.out.println("Enter Gender");
strGender = input.nextLine();
}

Answer

Create a class student with variables for each of the fields:

public class Student {
    public String strFirstName;
    public String strLastName; 
    public String strMajor;
    public int intGPA;
    public int intUIN;
    public String strNetID;
    public String strAge;
    public String strGender;

    public static void Student(String strFirstName, String strLastName, String strMajor, int intGPA, int intUIN, String strNetID, String strAge, String strGender) {
        this.strFirstName = strFirstName;
        this.strLastName = strLastName;
        this.strMajor = strMajor;
        this.intGPA = intGPA;
        this.intUIN = intUIN;
        this.strNetID = strNetID;
        this.strAge = strAge;
        this.strGender = strGender;
    }
}

To create a student:

Student mystudent = new Student(firstname, lastname, major, GPA, UIN, netID, age, gender);

To create an array of students:

Student[] myarray = new Student[length];

Adding students to the array:

myarray[0] = new Student(firstname, lastname, major, GPA, UIN, netID, age, gender);