So I'm reading from a text file with the following format
Type | ID | Name | Date | Doctor | Symptom
I have a method to reschedule an appointment where a promt is shown to enter an ID number, then it looks at each line in the text file to look for the ID entered. If the ID matches with the one in the text file... It shows the appointment and asks to enter a new date. If it doesn't match it says "no coming appointment for the entered ID"
The problem I'm having is that when it find the ID it prints that it doesn't match then later finds it.
It's clearly an issue with the nested for if loop.
Text File:
Emergency|32456|Mohammed Al Azri|12-11-2021 09:30|Dr. Muna Mousa|fever, cough
Routine|12345|Ali Al Abri|22-11-2021 10:30|Dr. Ahmed Al Abri|blood, x-ray
Routine|32456|Mohammed Al Azri|02-12-2021 08:45|Dr. Hisham Nazim|x-ray
Emergency|12345|Ali Al Abri|12-11-2021 08:15|Dr. Ahmed Al Abri|fever, cold, cough
Routine|43234|Mariam Ali|24-11-2021 09:15|Dr. Muna Mousa|blood, urine
Emergency|44342|Issa Ismail|13-11-2021 13:15|Dr. Muna Mousa|fever
Code Snippet:
static void rescheduleAppointment()
{
System.out.print("Enter Patient ID: ");
int id = input.nextInt();
input.nextLine();
for (int i = 0; i < schedule.size(); i )
{
if (id == schedule.get(i).getPatientID())
{
System.out.println("The coming scheduled appointment for " schedule.get(i).getPatientName() "(ID#: " schedule.get(i).getPatientID() ") on " schedule.get(i).getAppointmentTime());
System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
String dateAndTime = input.nextLine();
schedule.get(i).setAppointmentTime(dateAndTime);
System.out.println("Appointment has been updated");
}
else
{
System.out.println("No coming appointment found for " id ".");
System.out.println("You might need to schedule a new appointment.\n");
}
}
}
Output if input is 12345:
Enter Patient ID: 12345
No coming appointment found for 12345.
You might need to schedule a new appointment.
The coming scheduled appointment for Ali Al Abri(ID#: 12345) on 22-11-2021 10:30
Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm :
I'm guessing it sees the first line and ID doesn't match with the input, so it goes for the else statement, but in the next line when it matches it then goes for the if statement. What's the solution? Sorry in advance if the solution is very simple, I can't see it I've been coding all night :)
CodePudding user response:
One way to solve this is to create a boolean variable patientIdFound
that would represent whether or not the patient ID was found in the text file.
static void rescheduleAppointment()
{
System.out.print("Enter Patient ID: ");
int id = input.nextInt();
input.nextLine();
boolean patientIdFound = false;
for (int i = 0; i < schedule.size(); i )
{
if (id == schedule.get(i).getPatientID())
{
patientIdFound = true;
System.out.println("The coming scheduled appointment for " schedule.get(i).getPatientName() "(ID#: " schedule.get(i).getPatientID() ") on " schedule.get(i).getAppointmentTime());
System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
String dateAndTime = input.nextLine();
schedule.get(i).setAppointmentTime(dateAndTime);
System.out.println("Appointment has been updated");
break;
}
}
if (!patientIdFound) {
System.out.println("No coming appointment found for " id ".");
System.out.println("You might need to schedule a new appointment.\n");
}
}
I included break
statement to stop the for
loop once the patient ID is found.
CodePudding user response:
boolean found = false;
for (int i = 0; i < schedule.size(); i )
{
if (id == schedule.get(i).getPatientID())
{
System.out.printf("The coming scheduled appointment for %s(ID#: %s) on %s%n",
schedule.get(i).getPatientName(),
schedule.get(i).getPatientID(),
schedule.get(i).getAppointmentTime());
System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
String dateAndTime = input.nextLine();
schedule.get(i).setAppointmentTime(dateAndTime);
System.out.println("Appointment has been updated");
found = true;
break;
}
}
if (!found)
{
System.out.println("No coming appointment found for " id ".");
System.out.println("You might need to schedule a new appointment.");
System.out.println():
}
Only after every element was inspected, after the loop you can conclude failure.
Success can break
out of the loop.