0

The program begins with a prompt to:

  1. create a new list of students
  2. search for a student
  3. exit

Problem: In the second if statement oClassList is not initialized. How can I search the object array instantiated in the first if statement for lastName in the second?

    int z = 0;

    while(run) {

        if(z == 0) {
            System.out.println("Please choose an option.  (Enter 1, 2, or 3)\n");
        }
        else {
            System.out.println();
            System.out.println("Would you like to continue?  If so, please choose an option.\n");
        }

        System.out.println("1. New Class List");
        System.out.println("2. Search for a Student");
        System.out.println("3. Exit");

        iUserSelection = oScan.nextInt();


        Student[] oClassList;

        // creates new class list
        if(iUserSelection == 1) {

            System.out.println();

            System.out.println("How many students?");
            x = oScan.nextInt();

            System.out.println();


            // flush the buffer
            oScan.nextLine();

            oClassList = new Student[x];

            for(int i = 0; i < x; i++) {
                System.out.println("*********************");
                System.out.println("Student " + (i + 1) + " of " + x);
                System.out.println("*********************");


                oClassList[i] = new Student("","",0,0,0,0);


                System.out.print("First Name: ");
                oClassList[i].setFirstName(oScan.nextLine());

                System.out.print("Last Name: ");
                oClassList[i].setLastName(oScan.nextLine());

                System.out.print("Homework average: ");
                oClassList[i].setHWAve(oScan.nextInt());

                System.out.print("Quiz average: ");
                oClassList[i].setQuizAve(oScan.nextInt());

                System.out.print("Project average: ");
                oClassList[i].setProjectAve(oScan.nextInt());

                System.out.print("Test average: ");
                oClassList[i].setTestAve(oScan.nextInt());



                // flush the buffer
                oScan.nextLine();

                System.out.println();



                oClassList[i].printStudent();
            }
        }

        if(iUserSelection == 2) {
            // flush the buffer
            oScan.nextLine();

                System.out.println("Student search");

                System.out.print("Enter last name: ");
                sSearchLastName = oScan.nextLine();

                System.out.print("Enter first name: ");
                sSearchFirstName = oScan.nextLine();

                for(int y = 0; y >= oClassList.length; y++) {
                    if(sSearchLastName == oClassList[y].lastName) {
                        System.out.println("found elements");
                    }
                    else
                        System.out.println("Error - Student not found");
                }



        }

        if(iUserSelection == 3) {
            run = false;

            System.out.println();
            System.out.println("Goodbye.");
        }





        z++;
    }
0

1 Answer 1

1

Your variable Student[] oClassList; is a local variable, nested inside the while loop. This will cause the oClassList to be reset, everytime the program leaves the while scope (so each while processing will have its own class list).

You can just move that variable declaration above the while loop - and the scope of that variable will span around the whole program.

So just move Student[] oClassList; directly below int z = 0; and everything should work fine.


edit:

The scope is not everything, you also have to initialize the variable. A null check (as mentioned in another answer) is not a bad idea either.

int z = 0;

Student[] oClassList = null;


while(run) {

    if(z == 0) {
        System.out.println("Please choose an option.  (Enter 1, 2, or 3)\n");
    }
    else {
        System.out.println();
        System.out.println("Would you like to continue?  If so, please choose an option.\n");
    }

    System.out.println("1. New Class List");
    System.out.println("2. Search for a Student");
    System.out.println("3. Exit");

    iUserSelection = oScan.nextInt();


    // creates new class list
    if(iUserSelection == 1) {

        System.out.println();

        System.out.println("How many students?");
        x = oScan.nextInt();

        System.out.println();


        // flush the buffer
        oScan.nextLine();

        oClassList = new Student[x];

        for(int i = 0; i < x; i++) {
            System.out.println("*********************");
            System.out.println("Student " + (i + 1) + " of " + x);
            System.out.println("*********************");


            oClassList[i] = new Student("","",0,0,0,0);


            System.out.print("First Name: ");
            oClassList[i].setFirstName(oScan.nextLine());

            System.out.print("Last Name: ");
            oClassList[i].setLastName(oScan.nextLine());

            System.out.print("Homework average: ");
            oClassList[i].setHWAve(oScan.nextInt());

            System.out.print("Quiz average: ");
            oClassList[i].setQuizAve(oScan.nextInt());

            System.out.print("Project average: ");
            oClassList[i].setProjectAve(oScan.nextInt());

            System.out.print("Test average: ");
            oClassList[i].setTestAve(oScan.nextInt());



            // flush the buffer
            oScan.nextLine();

            System.out.println();



            oClassList[i].printStudent();
        }
    }

    if(iUserSelection == 2) {
        // flush the buffer
        oScan.nextLine();

            System.out.println("Student search");

            if (oClassList == null) {
                System.out.println("No class list defined yet");
            } else {

                System.out.print("Enter last name: ");
                sSearchLastName = oScan.nextLine();

                System.out.print("Enter first name: ");
                sSearchFirstName = oScan.nextLine();

                for(int y = 0; y >= oClassList.length; y++) {
                    if(sSearchLastName == oClassList[y].lastName) {
                        System.out.println("found elements");
                    }
                    else
                        System.out.println("Error - Student not found");
                }
            }



    }

    if(iUserSelection == 3) {
        run = false;

        System.out.println();
        System.out.println("Goodbye.");
    }





    z++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.