0

My piece of code will take a Patient object, and loop through an array which stores patient object and if it matches, it will print out the message on the if statement which is all good. But If the patient is not there, I believe the else segment will print out everytime the patient is not in the waitinglist array. What I'm trying to accomplish is to make the "your patient is not on the waiting list" print once if it's not in the array? Any idea how to do this? I tried to think about a way to do this, but I believe there is a simple solution that my brain cannot just figure out.

public int findWaitingPosition (Patient patient)
{
    for (int i=0 ; i <= waitingList.length-1 ; i++)
    {
        if (waitingList[i].equals(patient))
        {
            System.out.println ("The patient is on waiting list: " + i+1);
        }
        else
        {
            System.out.println ("Your patient is not on the waiting list");
        }

    }
9
  • 1
    If you find the name print it out and exit the method. Move the else clause outside the for statement Commented Nov 23, 2012 at 0:14
  • If i do that it will print the else clause even if it finds the patient, No? How would i exit the method? Commented Nov 23, 2012 at 0:17
  • If it finds a patient and you exit the method, the else clause (moved outside the for statement) will never be reached. So no, it won't print the else clause even if it finds the patient. Commented Nov 23, 2012 at 0:19
  • What's your method supposed to return? Commented Nov 23, 2012 at 0:24
  • Thanks I exited the method using return and now it seems like it will work. Method is supposed to return an int value (position on the waiting list) Commented Nov 23, 2012 at 0:29

2 Answers 2

2

I would use a temporary variable. Also it looks like your method is supposed to return the position of the patient in the array. In this snippet -1 means not found.

public int findWaitingPosition (Patient patient)
{
    int position = -1;
    for (int i=0 ; i <= waitingList.length-1 ; i++)
    {
        if (waitingList[i].equals(patient))
        {
            position = i;
            break;
        }
    }
    if (position >= 0)
        System.out.println ("The patient is on waiting list: " + i+1);
    else
        System.out.println ("Your patient is not on the waiting list");

    return position;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

You can change your loop as follows:

boolean patientNotInList = true;
for (int i=0 ; i <= waitingList.length-1 ; i++)
{
    if (waitingList[i].equals(patient))
    {
        System.out.println ("The patient is on waiting list: " + i+1);
        patientNotInList = false;
        break; //you might want to break the loop once the object is found
    }
}
if(patientNotInList) {
    System.out.println ("Your patient is not on the waiting list");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.