ok so my original code was:

Patient patient = new Patient();
        patient.setId(1);
        patient.setName("luke");
        patient.setAddress("100 test");
        patient.setTelNumber("01484710204");
        patient.healthproblem.setBrainProblemName("Brain Cancer");
        patient.healthproblem.setDateDiagnosed("30-03-1990");
        patient.healthproblem.setPatientHealth("Poor");

        Patient patient1 = new Patient();
        patient1.setId(2);
        patient1.setName("john");
        patient1.setAddress("100 test");
        patient1.setTelNumber("01484710204");
        patient1.healthproblem.setLungProblemName("Lung Cancer");
        patient1.healthproblem.setDateDiagnosed("12-02-1991");
        patient1.healthproblem.setPatientHealth("Poor");
        patient1.healthproblem.setLungProblemSide("Left");

if ("luke".equalsIgnoreCase(patient.getName())) {
                out.writeObject(patient);
            } else if ("john".equalsIgnoreCase(patient.getName())) {
                out.writeObject(patient1);
            }

so when the client typed ether luke or john it would return the data about them, now i have the patient data loading from a data file using the object outputsream to file. i created this to read in the object then return it:

public Object readPersons() throws IOException {
        Patient patient = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("PatientData.data"));
            for (int i = 0; i < 200; i++) {
                patient = (Patient) in.readObject();
            }
            in.close();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EOFException eof) {
        }
        return patient;

    }

but now the problem is what do i put where the write object is? and also how to loop round the file for the patients name :S

if ( ??? .equalsIgnoreCase(patient.getName())) {
                out.writeObject( ??? );
            }

thanks in advanced.
Houlahan

Recommended Answers

All 9 Replies

for (int i = 0; i < 200; i++) {
patient = (Patient) in.readObject();
}

Why do you need a for loop there? And why a hard-coded for loop at that? And why do you need to test to see if the patient's name is Luke anyway? If you're storing in a file system, all you should be doing is reading all of your data in or writing it all out (unless you only care about Patients named Luke, I guess).

yea i took the loop out after i realised it wasn't needed because that is a server side app and the client sends the name and the server then returns the object which corresponds with the name.
thanks.

So are you still having problems? If not, mark this as solved. If so, please explain your current problems further.

...

public Object readPersons() throws IOException {
        Patient patient = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("PatientData.data"));
            for (int i = 0; i < 200; i++) {
                patient = (Patient) in.readObject();
            }
            in.close();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EOFException eof) {
        }
        return patient;

    }

but now the problem is what do i put where the write object is? and also how to loop round the file for the patients name :S

if ( ??? .equalsIgnoreCase(patient.getName())) {
                out.writeObject( ??? );
            }

More clarification is needed here.

Are you trying to load the entire patient database (200 records assumed) as objects? If so, you will need to make an array of the patient type, and load them individually.

Patient [] patient = Patient[200]

Then, on the loop iteration through the file, you would do something like:

for (int i = 0; i < 200; i++) {
      patient[i] = (Patient) in.readObject();
   }

A sample search might look like:

int foundIndex = 0;
  // Seach through persons array...
for (int i = 0;i<200;i++) {
   if (patient[i].getName().equalsIgnoreCase("luke")) {
      foundIndex = i; // assign found object
      break;
   }
}

etc...

Does this sound like what you are trying to accomplish?

Another option that I think you may be wanting appears that your function is returning a singular object (patient) ,loaded from the file of 200 records. You are not checking for a name during the read, so this my not be correct..

More information is needed for this problem.

so when i type in to the server side luke it returns john and when i type in john it returns john it always returns the last object saved.

so when i type in to the server side luke it returns john and when i type in john it returns john it always returns the last object saved.

Take a look at your datafile, I'm sure you will find that the last patient object within it, belongs to john.

Nowhere in your code, are you creating separate instances of the Patient class! You are re-using a single instance of it.

private void returnPatient(Socket socket) throws IOException {
        ObjectOutputStream out = null;
        ObjectInputStream in = null;

        try {

            in = new ObjectInputStream(socket.getInputStream());
            Patient patient = (Patient) in.readObject();

            System.out.println("Message Recieved:" + patient.getName());

            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(readPersons(patient));

        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            System.out.println("class not found");
        } finally {
            in.close();
            out.close();
            socket.close();
        }
    }

    public Object readPersons(Patient patient) throws IOException {

        ObjectInputStream input = new ObjectInputStream(new FileInputStream("myobject.data"));
        Patient[] result = null;
        int i = 0;
        try {
            result = (Patient[]) input.readObject();
            while (true) {
                
                for (i = 0; i < 2; i++) {
                    if ((patient.getName()).equalsIgnoreCase(result[i].getName())) {
                        input.close();
                        return result[i];
                    }
                }                
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result[i];
    }

ok so i have this so far what im trying to do is when the user of the client types in the name say luke it sends the object to the server ive done that it receives the name but then i carnt use the methods i have above to then get the name object sent and compare it with the data in the file then send back the correct patient object

...
ok so i have this so far what im trying to do is when the user of the client types in the name say luke it sends the object to the server ive done that it receives the name but then i carnt use the methods i have above to then get the name object sent and compare it with the data in the file then send back the correct patient object

There are some elements to saving and loading of objects that you have not included. Do a Google search on "Java load save object".

Would it not be more efficient to send just the state (variable) portion of the object as a data stream instead?

commented: Good help. +4

:) thanks for all your help! ive solved the problem all working perfectly thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.