Reputation: 27
I am getting a user input and using a while loop to keep validating the input. However, no matter what type of input I enter that is supposed to be true, it keeps returning false and repeating the loop.
This is the section of the code that uses the loop:
String deletelName;
System.out.println("Type patient's last name to delete");
deletelName = cin.next();
Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
while (!bst.contains(removePatient)) {
System.out.println("Patient's last name does not exist. Type another last name : ");
deletelName = cin.next();
}
Part of the bst class:
public boolean contains(AnyType x)
{
return contains(x, root);
}
private boolean contains(AnyType x, BinaryNode<AnyType> t)
{
if (t == null)
return false;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
return contains(x, t.left);
else if (compareResult > 0)
return contains (x, t.right);
else
return true;
}
Upvotes: 1
Views: 821
Reputation: 7479
This will go on forever for a very obvious reason: you are not making a new patient each time because this line
Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
is not in the while loop, and therefore it always checks with the same Patient
. The solution is to replace this:
Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
while (!bst.contains(removePatient)) {
System.out.println("Patient's last name does not exist. Type another last name : ");
deletelName = cin.next();
}
With something like this:
Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
while (!bst.contains(removePatient)) {
System.out.println("Patient's last name does not exist. Type another last name : ");
deletelName = cin.next();
removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
}
Upvotes: 1
Reputation: 2172
removePatient isn't changing, only deletelName. So in order to fix your problem, add in removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);
at the end of your loop.
Upvotes: 1