The program begins with a prompt to:
- create a new list of students
- 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");
}
}