Results 1 to 4 of 4
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Storing undefined amount of objects into array

    I have been looking at my code, along with numerous other examples and none of it is making sense to me. I am not in school and this is not a project for anything in particular. I am just trying to learn on my own and sometimes what I read doesn't make sense. Hopefully someone here can enlighten me a little on this issue.

    SETUP: I have a start to a basic medical emergency program. At the beginning it asks if it is an emergency or if you want to enter information to store. So I started out with the Doctor class. I want to be able to store into an array, doctor objects that get created on the fly, then when a person says that it IS an emergency, I wanted that array[] to print out to show all the doctors, medications, etc.

    SO FAR: I have a main, and a Doctor class. The other lines have been commented out as they have not been created yet. I am able to create a doctor as I expected, but storing it into an array is confusing me.

    Hell, I don't even know if what I have so far is the best way to go about everything, though it does work to an extent.

    Doctor
    Java Code:
    package medicalrx;
    import java.util.Scanner;
    
    public class Doctor
    {                
              Scanner scan = new Scanner(System.in);
              String docName; // ********************************************************           Dr. name
              String aStreet; // ***********************************************************          Street Address
              String aCityState; // *******************************************************           City State
              String phone; // ************************************************************          Phone
               
              public void setDoctor()
              {
                        System.out.println("Doctor name -");
                        docName = scan.nextLine();
                        System.out.println("Street Address -");
                        aStreet = scan.nextLine();
                        System.out.println("City, State ZIP -");
                        aCityState = scan.nextLine();
                        System.out.println("Phone Number");
                        phone = scan.nextLine();
                }      
              public  String getDoctor()
              {
                        {return ("Dr. " + docName + "\nAddress: \n" + aStreet + "\n" + aCityState + "\n" + "Phone Number: "  + phone);}
               }
    }
    MAIN
    Java Code:
    package medicalrx;
    
    import java.util.Scanner;
              
    public class MedicalEmergency
    {
              public static void main(String[] args)
              {
                        System.out.println("\n\n\n*********************");
                        System.out.print("NOTE:");
                        System.out.println("This program CANNOT dial 9-1-1 or any other emergency number.");
                        System.out.println("If you have a real emergency, please contact 9-1-1 or your local emergency hotline.");
                        System.out.println("");
                        System.out.print("Is this an EMERGENCY ?");                              
                        System.out.println(" - (Y)es - (N)o - (E)nter information"); 
                        Scanner scan = new Scanner(System.in);
                        String input1 = scan.next(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////                  SCANNER
                        
                        if(input1.equalsIgnoreCase("N")) // *********************************************(N)O - NOT an emergency
                        {
                                  System.out.println("Accidents Happen. Be Careful!");
    
                        }
                        else if(input1.equalsIgnoreCase("Y")) // *****************************************(Y)ES - IS EMERGENCY
                        {
                                  System.out.println("*************************************************************");
                                  System.out.println("This program can display the following information");
                                  System.out.println("(1) - DOCTOR(S)\n(2) - CONDITION(S) / DISEASE(S)\n(3) - CURRENT MED(S)\n(4) - KNOWN ALLERGIES\n(5) - EMERCENY CONTACT");
                                  System.out.println("Please type a number to pull up that directory -");
                                  int input2 = scan.nextInt(); ///////////////////////////////////////////////////////////////////////////////////////////////////////              SCANNER
                                  
                                  if(input2==1)
                                  {
                                            System.out.println("DOCTORS-");
                                            System.out.println("Doctors Name");
                                            System.out.println("Address 1");
                                            System.out.println("Address 2");
                                            System.out.println("Phone Number");
                                                                         
                                  }
                        }
                        else if(input1.equalsIgnoreCase("E")) //******************************************** (E)NTER NEW USER DATA
                        {
                                  System.out.println("\n\n******************************");
                                  System.out.println("ADD NEW:");
                                  System.out.println("(1) Doctor");
                                  System.out.println("(2) Condition / Disease");
                                  System.out.println("(3) Medication");
                                  System.out.println("(4) Allergy");
                                  System.out.println("(5) Emergency Contact");
                                  int input3 = scan.nextInt(); ////////////////////////////////////////////////////////////////////////////////////////////////         SCANNER
                                  
                                  if(input3==1)
                                  {
                                            int addAnother = 1;
                                            
                                            while(addAnother==1)
                                            {
                                            System.out.println("\n\n\n************ Add New Doctor");
                                            Doctor doctor = new Doctor();
                                            doctor.setDoctor();
                                            System.out.println("Add another Doctor ? (Y) / (N)");
                                            String addDoc = scan.next();
                                            
                                            if(addDoc.equalsIgnoreCase("N"))
                                            {
                                                addAnother = 0;      
                                            }
                                               else if(addDoc.equalsIgnoreCase("Y"))
                                               {
                                                         addAnother = 1;
                                               }
                                                       else
                                                       {
                                                                System.out.println( 
                                                       }
                                            }
                                  }   
    //                              else if(input3==2)
    //                              {
    //                                        System.out.println("\n\n\n************ Add New Condition / Disease");
    //                                        Condition condition = new Condition();
    //                                        condition.setCondition();
    //                              }
    //                              else if(input3==3)
    //                              {
    //                                        System.out.println("\n\n\n************ Add New Condition / Disease");
    //                                        Medication medication = new Medication();
    //                                        medication.setMedication();
    //                              }
    //                               else if(input3==4)
    //                              {
    //                                        System.out.println("\n\n\n************ Add New Condition / Disease");
    //                                        Allergy allergy = new Allergy();
    //                                        allergy.setAllergy();
    //                              }
    //                               else if(input3==5)
    //                              {
    //                                        System.out.println("\n\n\n************ Add New Condition / Disease");
    //                                        Contact contact = new Contact();
    //                                        contact.setContact();
    //                                }
                        }
                        else
                        {
                                  System.out.println("Is this an Emergency, (Y) or (N) ?"); // ****************************ASK IF EMERGENCY AGAIN
                                  input1 = scan.next();
                        }
              }
    }
    I have been wanting to switch to Eclipse to see if that would help me out any in regards to autofilling certain areas, but as of yet I am still coding in Dr. Java until I can get more basics down.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Storing undefined amount of objects into array

    Can you explain what your problem(s) are?

    One suggestion: You could use a switch() statement instead of the chain of if/else if statements
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Storing undefined amount of objects into array

    Java Code:
    package medicalrx;
    
    public class DoctorList 
              {
              public static Doctor[] doctorList = new Doctor[10];
              private int i = 0;
              
              public void add(Doctor doctor)
              {
                        if(i<doctorList.length)
                        {
                                  doctorList[i]=doctor;
                                  System.out.println("Doctor Added at index: " + i);
                                  i++;
                        }
              }
    }
    I think I found some code somewhere to study and break down. As for question though, I am still confused on printing out the information (getDoctor) when I call the list of Doctors up. I see the memory slots and null's but I havent looked to deep into this though. Thanks for the switch() information. Off to read some more.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Storing undefined amount of objects into array

    printing out the information (getDoctor) when I call the list of Doctors up. I see the memory slots and null
    The default value for a variable like String is null and for slots in an array. Make sure all the variables are assigned a value.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 11-02-2011, 04:38 AM
  2. Storing objects in an array list?
    By Moedig in forum New To Java
    Replies: 3
    Last Post: 10-27-2011, 07:39 PM
  3. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 07:12 AM
  4. Vectors - accessing an unknown amount of objects
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 11:45 PM
  5. Constructor with unknown amount of objects?
    By Bernard Robitaille in forum New To Java
    Replies: 5
    Last Post: 03-01-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •