0

I'm trying to make an array of objects I have a counter (totalNum) in a class named Patient

the problem is when ever I add information the information that is in the previous array is gone and all the arrays will take the value of the new information any suggestion on how i can solve this problem ?

p.s (the increment of the counter is in this constructor ) hospitalPatient[Patient.totalNum] = new Patient(ispnMain , name ,age ,bloodTypeMain);

and this is my code: the code is in a while loop so the user will enter a choice and then it will ask for this information

 for (int j = 0; j < 100; j++) {
   hospitalPatient[j] = new Patient();
 }

 while(choice != 7) {
   switch (choice) {
     case 1:
       System.out.println ("Please Enter the patient's ISPN:" );
       String ispnMain = read.next();
       System.out.println ("Please Enter the patient's name:" );
       String name = read.next();
       System.out.println ("Please Enter the patient's Age:" );
       int age = read.nextInt();
       System.out.println ("Please Enter the patient's Blood Type:" );
       String bloodTypeMain = read.next();


       hospitalPatient[Patient.totalNum] = new Patient(ispnMain , name ,age ,bloodTypeMain);

       hospitalPatient[Patient.totalNum].SetISPN(ispnMain);
       hospitalPatient[Patient.totalNum].SetName(name);
       hospitalPatient[Patient.totalNum].SetAge(age);
       hospitalPatient[Patient.totalNum].SetBloodType(bloodTypeMain);

       hospitalPatient[Patient.totalNum].getISPN();
       hospitalPatient[Patient.totalNum].getName();
       hospitalPatient[Patient.totalNum].getAge();
       hospitalPatient[Patient.totalNum].getBloodType();

    // other cases here
    }
}
10
  • Do you increase totalNum in the constructor ? Commented Dec 18, 2014 at 16:36
  • Can you explain why you do both hospitalPatient[j] = new Patient(); and hospitalPatient[Patient.totalNum] in the same loop? Commented Dec 18, 2014 at 16:37
  • Aren't those lines: hospitalPatient[Patient.totalNum].SetISPN(ispnMain); hospitalPatient[Patient.totalNum].SetName(name); hospitalPatient[Patient.totalNum].SetAge(age); hospitalPatient[Patient.totalNum].SetBloodType(bloodTypeMain); parts of this constructor: hospitalPatient[Patient.totalNum] = new Patient(ispnMain , name ,age ,bloodTypeMain); ? Commented Dec 18, 2014 at 16:38
  • You don't show us where you increment totalNum, why are you hiding it? Commented Dec 18, 2014 at 16:42
  • demostene :yes i'm sure is it increasing Commented Dec 18, 2014 at 16:44

4 Answers 4

2

You could switch to ArrayList and just call add(new Patient()). You will avoid all problems with incorrect indices and array resize.

Sign up to request clarification or add additional context in comments.

Comments

0

there must be a problem in the Patient Constructor. Are you sure that the incremented variable is really incrementing and not overwritten each time yo call the static totalNum again?

Anyway,

you should a) use an ArrayList for that and b) add it on the fly. Everything else is overload for the memory.

ArrayList<Patient> hospitalPatients = new ArrayList<>();


 while(choice != 7) {
 switch (choice) {
 case 1 :
 System.out.println ("Please Enter the patient's ISPN:" );
 String ispnMain = read.next();
 System.out.println ("Please Enter the patient's name:" );
 String name = read.next();
 System.out.println ("Please Enter the patient's Age:" );
 int age = read.nextInt();
 System.out.println ("Please Enter the patient's Blood Type:" );
 String bloodTypeMain = read.next();



Patient patient = new Patient(ispnMain , name ,age ,bloodTypeMain);
patient.SetISPN(ispnMain);
patient.SetName(name);
.... and so on.

hospitalPatients.add(patient);
}

You can get the patient with an index. hospitalPatients.get(patientNumberic) or you can get all by looping through the list.

 for(Patient patient  : hospitalPatients) {
     patient.get(....);
 }

1 Comment

yes I'm sure that the counter is incrementing since i printed before and it was okay.
0

You don't want to use a counter in your patient constructor. Since you are creating all of the patient objects at the loop at the beginning the counter will increment 100 times then and never increment again. You should move the counter to your input loop.

Since it appears to be possible to input more than 100 patients on the input loop (it keeps going until the user tells it to quit) you may want to use an arraylist instead. This will allow you to enter as many as you want. It also would not require a counter.

Comments

0

I think I miss understood question at very first glance. Any way waiting for others to comment on this

1 Comment

j is only used to initialze the array of Patient.

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.