I am having problem with my Search Method once the item i am searching for is shown in this case its the students details, it outputs the “else” option of the if statment, below is the code that is being used in this method
public void search(String StudentlName)
{
boolean found = true; // set flag to true to begin first pass
while ( found ==true )
{
for (int index = 0; index<= lName.length; index ++)
{
if (StudentlName.equalsIgnoreCase(lName[index])
{
System.out.println(course+"\n"+
"Student ID = \t"+index+"\n"+
unitTitle + "\n" +
fName[index] + "\n" +
lName[index] + "\n" +
Marks[index] + "\n" + "\n" );
found = false;
}
else
{
System.out.println("Student Not Found");
}//End of If
}// End of For
}
}//end of search Method
This is part of my Menu Class,
case 7:
System.out.println("Please Enter The Students
you wish to find Last Name ");
String templName = keyb.nextLine();
System.out.println("");
myUnit.search(templName);
option = menuSystem();
break;
I figure its got something to do with the for loop but i cant get it through my head.
Once i type in the correct Surname (in this case “Scullion”) I want in this appears:
HND Computing
Student ID = 0
Java
Daniel
Scullion
60
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
10 Student Not Found
Student Not Found
Student Not Found
Student Not Found
Student Not Found Student Not Found Student Not Found Student Not
Found Student Not Found
should be
Array indexes are zero based. i.e, they
start index is 0 and the end-index is Arraylength-1.Example: for isntance your array is of length 10.
start-Index—>0
end-index—–>9
If you try to access an index above 9, ArrayIndexOutOfBounds will be thrown at runtime.
EDIT:
use a break statement to break out of your for-loop once you have found the student.