java - Second record doesnt get stored in arraylist -


i have arraylist records stored objects. in different form allow user enter id , retrieve data of record corresponding id.

my problem , can retrieve 1 record , meaning first record store in arraylist.

if type in second record , if try search record using id , message "invalid id", message assigned make sure users won't enter invalid ids.

here code used store object in arraylist:-

patient_class patients=new patient_class(firstname,lastname,initials,gender,birthday,birthmonth,birthyear,contactnumber,address,bloodgroup,patientid);             patientlist.add(patients); 

here code check whether if arraylist contains id.

public boolean checkrecord(arraylist<patient_class>patients,string search)     {           for(patient_class patient : patients)          {         if(patient.getid().contains(search))         {             return true;         }      }         return false;     } 

if true have created separate constructor find record give id.

here code :-

public patient_class searchrecord(arraylist<patient_class> patients, string search)  {           for(patient_class patient: patients) //used  enhanced loop               if(patient.getid().equals(search))             {                  return patient;              }         else             {             return null;             }         return null;         } 

why can enter 1 record not 2 records in arraylist ? program display "succussfuly registered" when enter second record , click register, cant search record , can delete record using method made.

what doing wrong ?

this problem:

for(patient_class patient: patients) //used  enhanced loop     if(patient.getid().equals(search))     {         return patient;     }     else     {         return null;     } return null; 

due else block, you're returning null if first patient doesn't match patient you're looking for, instead of looking other matches. should rid of else block. i'd add braces make control flow clearer:

for (patient_class patient : patients) {     if (patient.getid().equals(search)) {         return patient;     } } // return null if we've checked *all* patients return null; 

additionally, i'd strongly advise start following java naming conventions, renaming patient_class patient , getid method getid:

for (patient patient : patients) {     if (patient.getid().equals(search)) {         return patient;     } } return null; 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -