Reputation: 58
I have a quick question and I couldn't find any information on it in the old posts. It has to do with my while loop. I want my while loop to end when the user inputs the enter key, i.e. when the patient ID is empty. The while loop terminates if i hit enter for the first round of input, but if i input a lot of information and run the loop a few times hitting enter doesn't end the loop when it asks for patient ID again. Here is the code. I've tried many different variations for the while(). Thank you for the help!
//Obtain Patient Id
System.out.print("Enter patient: ");
patientID= kb.next();
//While patient id is not empty
while(patientID.length()==0) {
// Obtain patient ID
System.out.print("\nEnter patient: ");
patientID=kb.next();
//End While
Upvotes: 0
Views: 367
Reputation: 41281
There are some issues. You need to use nextLine()
to read a line. next()
just reads a token.
patientID=kb.nextLine();
//While patient id is not empty
while(patientID.length()!=0) {
// Obtain patient ID
System.out.print("\nEnter patient: ");
patientID=kb.nextLine();
}
The while loop should also read "while patient ID is NOT empty". You'll need to set patientID
to a non-empty value before the loop starts as I did here.
Upvotes: 1