java - Infinite loop when trying to populate a list of no more than two Hospitals -


in project user must enter 1 or 2 hospitals not 3 or more. program starts , display menu. if user presses 1 must enter hospital(name , department). after program displays menu again , user can choose insert hospital.

but after that, if choose insert 1 (which not permitted) program accepts it. seems every time inserthospitals() called main class, value of numberofhospitals (which counter counting how many hospitals entered) equals 0.

public class hospital {     private string name, departments;     private char flag;     private int numberofhospitals;      private hospital[] listofhospitals;     //private patient[] listofpatiens;     //private doctor[] listofdoctors;     //private examination[] listofexaminations;     //private folder[] listoffolders;      public hospital(string name, string departments)     {         this.name=name;         this.departments=departments;     }     public hospital()     {         listofhospitals = new hospital[2];         //listofpatiens = new patient[100];         //listofdoctors = new doctor[100];         //listofexaminations = new examination[100];         //listoffolders = new folder[100];     }     public string getname()     {         return name;     }     public void setname(string name)     {         this.name=name;     }     public string getdepartments()     {         return departments;     }     public void setdepartments(string departments)     {         this.departments=departments;     }       public void inserthospitals()     {         if(numberofhospitals==2)         {            system.out.println("you can give 2 hospitals!");          }         else         {              string temp = sir.readstring("hospital's name:");             name=temp;             string temp1 = sir.readstring("hospital's departments:");             departments=temp1;             hospital hospital = new hospital(name, departments);             listofhospitals[numberofhospitals]=hospital;             numberofhospitals=numberofhospitals+1;                   }     } } 

your misunderstanding something, list of hospitals (as mentioned) should not inside hospital class. have consider hospital class blueprint using in application.

which means need have list of hospitals, list inside other application class (which runs application) , inserthospitals method should not in hospital class either obviously.

as add new hospital in program, create new hospital object , add list of hospitals (fx arraylist) keeping field value.

also posssibly make new constructor parameters in hospital class can insert values outside of class.

something fx.

public class mainapp {     private arraylist<hospital> hospitallist;      public static void main(string[] args) {         // initialize or load file or whatever here.         hospitallist = new arraylist<hospital>();         // code here...     }      public void inserthospital(<insert parameters here create hospital>) {         hospital newhospital = new hospital(<insert params new constructor>);         hospitallist.add(newhospital);      } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -