Can't print found element in array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Can't print found element in array

  1. #1
    Join Date
    Apr 2004
    Posts
    16

    Unhappy Can't print found element in array

    Can anyone PLEASE help! It's driving me round the bend. I have set up an array asking the user to enter the array value. There are several methods that all work well except one - find element in array.

    I think it is finding the element, but I am unable to print the details of the found element, it prints the details of the last element added. The find method asks the user to enter the string value to be found in the array, returning either Found with the details printed or not found.

    The methods that work let me add an element to the array, delete an element from the array & fills the gaps (which to add to the confusion uses the find method), and print all element details in array.

    I can provide the code if anyone can help, but it is part of a large menu so didn't want to overload the question!

    Any suggestions will be very gratefully received.

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    You'll need to provide all the relevant code or we'll never be able to know whats going on

  3. #3
    Join Date
    Apr 2004
    Posts
    16

    Code....

    Here goes...I've just put the code that relates to the problem method or is called by it......thanx

    public boolean addPatient(Patient p)
    {
    if (numPatients < maxPatients) //if array isn't full
    {
    patientList[numPatients] = p; //creates new patient
    numPatients++; //increments patient number
    return true;
    }
    else
    return false;
    }

    public void findPatient() //think this is the problem method!
    {
    String patientName = Console.readString("What name? ");
    Patient pat = getPatient(patientName);
    if (pat != null)
    {
    System.out.println("Found");
    patientList[numPatients-1].printDetails();
    //this bit is wrong - prints last added patient not the one found
    }
    else
    System.out.println("Patient not found");
    }

    private int findPatient(String patientName)
    {
    for (int i=0; i<numPatients; i++)
    {
    if (patientList[i].getPatientName().equalsIgnoreCase(patientName))
    return i;
    }
    return -1;
    }

    public Patient getPatient(String patientName)
    {
    int pos = findPatient(patientName);
    if (pos != -1)
    return patientList[pos];
    else
    return null;
    }

    public boolean deletePatient(String patientName)
    {
    int pos = findPatient(patientName);
    if (pos != -1)
    {
    numPatients--; //takes away patient from array numbers
    for (int i=pos; i<numPatients; i++)
    patientList[i] = patientList[i+1]; //shuffles array to fill gap
    return true;
    }
    else
    return false;
    }

    public void printPatientList()
    {
    System.out.println("Patients currently registered:");
    for (int i=0; i<numPatients; i++)
    patientList[i].printDetails(); //prints all patients in array
    }

    public void deletePatient()
    {
    String patientName = Console.readString("What name?");
    boolean success = deletePatient(patientName);
    if (success)
    {
    System.out.println("Patient deleted");
    }
    else
    System.out.println("Patient not found");
    }

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    Firstly, when you post code you should use [ CODE ] and [ /CODE ] tags (without the spaces between the braces) to format the code.

    Code:
    public void findPatient() //think this is the problem method!
    {
        String patientName = Console.readString("What name? ");
        Patient pat = getPatient(patientName);
        if (pat != null)
        {
            System.out.println("Found");
            patientList[numPatients-1].printDetails();
            //this bit is wrong - prints last added patient not the one found
        }
        else
        {  
            System.out.println("Patient not found");
        }
    }
    If you look at the bold part in your code above, that refers to a patient object in the array. The problem is that patientList[numPatients-1] is a constant (assuming that no patients are added or removed, but since this is a search method that won't happen). If there are 10 patients in an array, then the search method will only ever print out the last patient at index 9. I think you should replace that part of the code with pat, the patient returned from the search.

    Code:
    public void findPatient() //think this is the problem method!
    {
        String patientName = Console.readString("What name? ");
        Patient pat = getPatient(patientName);
        if (pat != null)
        {
            System.out.println("Found");
            pat.printDetails();
            //this bit is wrong - prints last added patient not the one found
        }
        else
        {  
            System.out.println("Patient not found");
        }
    }

  5. #5
    Join Date
    Apr 2004
    Posts
    16
    Thanks very much Mike, that works a treat!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center