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
}
}
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);?totalNum, why are you hiding it?