kintzee619
kintzee619

Reputation: 13

Issue using methods and an array

My problem here is displaying the data using another method. I tried both of the methods but there was no output.

I think that objects in the ArrayList are gone when I made them as parameters on both methods or maybe not.

Please kindly help me with this problem of mine. There are still more options to be filled, I also need some help for it.

public class Student {
    private String IDNumber;
    private String firstName;
    private String middleName;
    private String lastName;
    private String degree;
    private int yearLevel;

    public Student() {
        this.IDNumber = IDNum;
        this.firstName = fName;
        this.middleName = mName;
        this.lastName = lName;
        this.degree = deg;
        this.yearLevel = level;
    }

    public void setIdNumber(String IDNumber) {
        this.IDNumber = IDNumber;
    }

    public String getIdNumber() {
        return IDNumber;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getMiddleName()
    {
        return middleName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public String getDegree() {
        return degree;
    }

    public void setYearLevel(int yearLevel) {
        this.yearLevel = yearLevel;
    }

    public int getYearLevel() {
        return yearLevel;
    }

    /* @Override
    public String toString(){
        return ("ID Number: "+this.getIdNumber()+
                    "\nName: "+ this.getFirstName()+
                    " "+ this.getMiddleName()+
                    " "+ this.getLastName()+
                    "\nDegree and YearLevel: "+ this.getDegree() +
                    " - " + this.getYearLevel());
    } */

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

public class test {

    public static void main(String[] args) {
        menu();
    }

    public static void menu() {
        Scanner in = new Scanner(System.in);

        int choice = 0;
        System.out.print("****STUDENT RECORD SYSTEM****\n\n");
        System.out.println("\t MENU ");
        System.out.println("[1]ADD STUDENT");
        System.out.println("[2]DISPLAY ALL");
        System.out.println("[3]DISPLAY SPECIFIC");
        System.out.println("[4]UPDATE");
        System.out.println("[5]AVERAGE");
        System.out.println("[6]EXIT");
        System.out.println("?");

        choice = in.nextInt();
        if (choice == 1) {
            options();
        }

        else if (choice == 2) {
            displayAll(student, studentList);
        }

        else if (choice == 3) {
            displaySpecific(student, studentList);
        }

    }

    public static void options() {
        Scanner in = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<Student>();
        char ans;
        String temp;

        int total;

        do {

            System.out.println("TOTAL: ");
            total = in.nextInt();

            Student[] student = new Student[total];

            for (int index = 0; index < student.length; index++) {
                student[index] = new Student();

                System.out.print("*********STUDENT INFORMATION*********\n\n");
                System.out.println("PRESS ENTER");
                in.nextLine();
                System.out.print("ID NUMBER: ");
                student[index].setIdNumber(in.nextLine());

                System.out.print("FIRST NAME: ");
                student[index].setFirstName(in.nextLine());

                System.out.print("MIDDLE NAME: ");
                student[index].setMiddleName(in.nextLine());

                System.out.print("LAST NAME: ");
                student[index].setLastName(in.nextLine());

                System.out.print("DEGREE: ");
                student[index].setDegree(in.nextLine());

                System.out.print("YEAR LEVEL: ");
                student[index].setYearLevel(in.nextInt());

                studentList.add(student[index]);

            }

            System.out
                    .print("Would you like to enter in a new student (y/n)? ");
            String answer = in.next();
            ans = answer.charAt(0);

        } while (ans == 'y');

        // SEARCH and DISPLAY SPECIFIC
        String id = new String();
        in.nextLine();
        System.out.print("Enter ID NUMBER: ");
        id = in.nextLine();

        for (int j = 0; j < studentList.size(); j++) {
            if (id.equals(studentList.get(j).getIdNumber())) {
                System.out.printf("STUDENT SEARCHED");
                System.out.print("\nID NUMBER:             "
                        + studentList.get(j).getIdNumber());
                System.out.print("\nFULL NAME: "
                        + studentList.get(j).getFirstName() + " "
                        + studentList.get(j).getMiddleName() + " "
                        + studentList.get(j).getLastName());
                System.out.print("\nDEGREE and YEAR: "
                        + studentList.get(j).getDegree() + "-"
                        + studentList.get(j).getYearLevel() + "\n\n");
                System.out.println();
            }
        }

        // DISPLAY ALL
        for (int i = 0; i < studentList.size(); i++) {
            System.out.printf("STUDENT[%d]", i + 1);
            System.out
                    .print("\nID NUMBER: " + studentList.get(i).getIdNumber());
            System.out.print("\nFULL NAME: "
                    + studentList.get(i).getFirstName() + "   "
                    + studentList.get(i).getMiddleName() + " "
                    + studentList.get(i).getLastName());
            System.out.print("\nDEGREE and YEAR: "
                    + studentList.get(i).getDegree() + "-"
                    + studentList.get(i).getYearLevel());
            System.out.println();
        }

        menu();

    }

    public static void displayAll(Student student,
            ArrayList<Student> studentList) {
        System.out.printf("STUDENT RECORD");

        for (int i = 0; i < studentList.size(); i++) {
            System.out.printf("STUDENT[%d]", i + 1);
            System.out
                    .print("\nID NUMBER: " + studentList.get(i).getIdNumber());
            System.out.print("\nFULL NAME: "
                    + studentList.get(i).getFirstName() + " "
                    + studentList.get(i).getMiddleName() + " "
                    + studentList.get(i).getLastName());
            System.out.print("\nDEGREE and YEAR: "
                    + studentList.get(i).getDegree() + "-"
                    + studentList.get(i).getYearLevel());
            System.out.println();
        }
    }

    public static void displaySpecific(Student student,
            ArrayList<Student> studentList) {
        String id = new String();
        in.nextLine();
        System.out.print("Enter ID NUMBER: ");
        id = in.nextLine();

        for (int j = 0; j < studentList.size(); j++) {
            if (id.equals(studentList.get(j).getIdNumber())) {
                System.out.printf("STUDENT SEARCHED");
                System.out.print("\nID NUMBER: "
                        + studentList.get(j).getIdNumber());
                System.out.print("\nFULL NAME: "
                        + studentList.get(j).getFirstName() + " "
                        + studentList.get(j).getMiddleName() + " "
                        + studentList.get(j).getLastName());
                System.out.print("\nDEGREE and YEAR: "
                        + studentList.get(j).getDegree() + "-"
                        + studentList.get(j).getYearLevel() + "\n\n");
                System.out.println();
            }
        }
    }
}

Upvotes: 1

Views: 118

Answers (1)

Marco Acierno
Marco Acierno

Reputation: 14847

Move

ArrayList <Student> studentList = new ArrayList <Student>();

from options method to class field.

EDIT:

As you can see, now studentList is not a local variable of

public static void options()

but of the class.

Anyway i edited the args of some methods because you don't need to pass students as argument since now it's a field of the class.

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

public class test {
    static ArrayList<Student> studentList = new ArrayList<Student>();

    public static void main(String[] args) {
        menu();
    }

    public static void menu() {
        Scanner in = new Scanner(System.in);

        int choice = 0;
        System.out.print("****STUDENT RECORD SYSTEM****\n\n");
        System.out.println("\t MENU ");
        System.out.println("[1]ADD STUDENT");
        System.out.println("[2]DISPLAY ALL");
        System.out.println("[3]DISPLAY SPECIFIC");
        System.out.println("[4]UPDATE");
        System.out.println("[5]AVERAGE");
        System.out.println("[6]EXIT");
        System.out.println("?");

        choice = in.nextInt();
        if (choice == 1) {
            options();
        }

        else if (choice == 2) {
            displayAll();
        }

        else if (choice == 3) {
            displaySpecific(student);// here  you should ask to the user what studend he want to show - here it continues to give you error
        }

    }

    public static void options() {
        Scanner in = new Scanner(System.in);
        char ans;
        String temp;

        int total;

        do {

            System.out.println("TOTAL: ");
            total = in.nextInt();

            Student[] student = new Student[total];

            for (int index = 0; index < student.length; index++) {
                student[index] = new Student();

                System.out.print("*********STUDENT INFORMATION*********\n\n");
                System.out.println("PRESS ENTER");
                in.nextLine();
                System.out.print("ID NUMBER: ");
                student[index].setIdNumber(in.nextLine());

                System.out.print("FIRST NAME: ");
                student[index].setFirstName(in.nextLine());

                System.out.print("MIDDLE NAME: ");
                student[index].setMiddleName(in.nextLine());

                System.out.print("LAST NAME: ");
                student[index].setLastName(in.nextLine());

                System.out.print("DEGREE: ");
                student[index].setDegree(in.nextLine());

                System.out.print("YEAR LEVEL: ");
                student[index].setYearLevel(in.nextInt());

                studentList.add(student[index]);

            }

            System.out
                    .print("Would you like to enter in a new student (y/n)? ");
            String answer = in.next();
            ans = answer.charAt(0);

        } while (ans == 'y');

        // SEARCH and DISPLAY SPECIFIC
        String id = new String();
        in.nextLine();
        System.out.print("Enter ID NUMBER: ");
        id = in.nextLine();

        for (int j = 0; j < studentList.size(); j++) {
            if (id.equals(studentList.get(j).getIdNumber())) {
                System.out.printf("STUDENT SEARCHED");
                System.out.print("\nID NUMBER:             "
                        + studentList.get(j).getIdNumber());
                System.out.print("\nFULL NAME: "
                        + studentList.get(j).getFirstName() + " "
                        + studentList.get(j).getMiddleName() + " "
                        + studentList.get(j).getLastName());
                System.out.print("\nDEGREE and YEAR: "
                        + studentList.get(j).getDegree() + "-"
                        + studentList.get(j).getYearLevel() + "\n\n");
                System.out.println();
            }
        }

        // DISPLAY ALL
        for (int i = 0; i < studentList.size(); i++) {
            System.out.printf("STUDENT[%d]", i + 1);
            System.out
                    .print("\nID NUMBER: " + studentList.get(i).getIdNumber());
            System.out.print("\nFULL NAME: "
                    + studentList.get(i).getFirstName() + "   "
                    + studentList.get(i).getMiddleName() + " "
                    + studentList.get(i).getLastName());
            System.out.print("\nDEGREE and YEAR: "
                    + studentList.get(i).getDegree() + "-"
                    + studentList.get(i).getYearLevel());
            System.out.println();
        }

        menu();

    }

    public static void displayAll() {

        for (int i = 0; i < studentList.size(); i++) {
            System.out.printf("STUDENT[%d]", i + 1);
            System.out
                    .print("\nID NUMBER: " + studentList.get(i).getIdNumber());
            System.out.print("\nFULL NAME: "
                    + studentList.get(i).getFirstName() + " "
                    + studentList.get(i).getMiddleName() + " "
                    + studentList.get(i).getLastName());
            System.out.print("\nDEGREE and YEAR: "
                    + studentList.get(i).getDegree() + "-"
                    + studentList.get(i).getYearLevel());
            System.out.println();
        }
    }

    public static void displaySpecific(Student student) {
        String id = new String();
        in.nextLine();
        System.out.print("Enter ID NUMBER: ");
        id = in.nextLine();

        for (int j = 0; j < studentList.size(); j++) {
            if (id.equals(studentList.get(j).getIdNumber())) {
                System.out.printf("STUDENT SEARCHED");
                System.out.print("\nID NUMBER: "
                        + studentList.get(j).getIdNumber());
                System.out.print("\nFULL NAME: "
                        + studentList.get(j).getFirstName() + " "
                        + studentList.get(j).getMiddleName() + " "
                        + studentList.get(j).getLastName());
                System.out.print("\nDEGREE and YEAR: "
                        + studentList.get(j).getDegree() + "-"
                        + studentList.get(j).getYearLevel() + "\n\n");
                System.out.println();
            }
        }
    }
}

Upvotes: 2

Related Questions