Reputation:
The program begins with a prompt to:
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");
}
}
Upvotes: 0
Views: 137
Reputation: 34146
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)
// ...
// ...
}
Upvotes: 0
Reputation: 13
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");
}
}
Upvotes: 1
Reputation: 595
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.
Upvotes: 0