| trantommyd |
03-16-2012 09:39 PM |
"Student Not Found" Problem
I'm trying to display a message that informs the user that the name he entered was not found in the list of students. I wrote a binary search to go through the list of names to compare to the search key.
Code:
while(repeat)
{
searchEntry = JOptionPane.showInputDialog("Enter the last name of the student you wish to see the grade of: ");
if(searchEntry.length() == 0)
{
System.exit(0);
}
int low = 0;
int high = studentArray.length - 1;
while(high >= low)
{
int mid = (low+high)/2;
if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) < 0)
{
high = mid - 1;
}
else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) > 0)
{
low = mid + 1;
}
else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) == 0)
{
JOptionPane.showMessageDialog(null, studentArray[mid].firstName + " " + studentArray[mid].getLastName()
+ " received a grade of " + studentArray[mid].getNumberGrade());
high = -1;
}
else
{
JOptionPane.showMessageDialog(null, "Name not found.");
}
}
}
Here, I'm using an if-else-if statement, but it is not working the way I want it to.
|