0

The program begins with a prompt to:

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

Problem: I create an object array and populate it in the first if statement then try to access it in the second if statement, which I know I can't do. So how can I create and populate the array of objects and access it later? Any ideas?

if(iUserSelection == 1) {

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

    // flush the buffer
    oScan.nextLine();

    Student[] 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();

    if(oClassList[0] != null) { 
        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");
    }
}

3 Answers 3

1

To keep the array from being deleted when the if-statement exits, declare it outside of the if-statement, giving it a wider scope. The array can then be populated inside of the if-statement without going outside of scope when the if statement exits. For example,

int[] arr;
if (true) {
    arr = new int[1];
    arr[0] = 5;
}
System.out.println(arr[0]);

Output:

5

arr will maintain its value when the exiting the if-statement because it was declared outside of the if-statement and then instantiated inside.

Your corrected code would be:

Student[] oClassList;                //Declared outside of both if-statements

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();

    if(oClassList[0] != null) { 
        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");
    }


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

Comments

0

You already know your answer. It is a scope issue, so the solution is define your array in a "wider" scope, that your second if also sees. So, basically define it before if statements.

Comments

0

Scope is defined by blocks, a block is delimited by brackets {}. If you create the array inside the if block, you won't be able to access to it in the other if block.

How to solve this? You can declare the array in an outer block, so you can access to it in all that block.

Student[] oClassList = null;

if (iUserSelection == 1) {
    // ...
    oClassList = new Student[x];
    // ...
}

if (iUserSelection == 2) {
    if(oClassList != null)
        // ...
    // ...
}

4 Comments

Great. I can do that but x is defined in the first if statement.
@SkilletSpecial Edited to fit your requirements. You can declare the array first, and then when you have x you can initialize it. Note that if you do this, you can get null if you enter the second ifand the array was not initalized yet. To check this you can use if (oClassList != null)
Ok I declared the array first. The only remaining issue is in the second if statement. The 'local variable oClassList may not have been initialized.' But if I initialize Student[] oClassList = null; then I get a NullPointerException.
Yes, that's why you have to check if (oClassList != null) { // do some operations with oClassList}.

Your Answer

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