Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing an unwanted else statement in a for loop

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 🙂

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading