1

I'm trying to write a that program asks the user to enter the number of patients to be saved in a hospital's database, which is ArraySize . The user must enter the patient's ID, first and last name, and then the patient's ID, first and last names are indexed in the array according to the severity of his/her condition. My question is how do I save the user's ID input to be a part of the entire ID array, whose limit is as entered by the user? How do I index the patient's information?

Here's my code so far:

System.out.print("\n > How many patients allowed for day 25-12-2016 : ");
        int ArraySize = input.nextInt();

 System.out.print("\n       > Enter patient ID: ");
                int ID[] = new int[ArraySize];

                for (int i = 0; i < ID.length; i++) {
                    ID[i] = input.nextInt();
                }


   System.out.print("\n         > Enter patient First Name: ");
            String first[] = new String[ArraySize];
            for (int i = 0; i < first.length; i++) {
                first[i] = reader.nextLine();
            }
            System.out.print("\n        > Enter patient last Name: ");
            String last[] = new String[ArraySize];
            for (int i = 0; i < last.length; i++) {
                last[i] = reader.nextLine();
            }

 System.out.print("\n _______________________________________________\n"
                + "|                                               |\n"
                + "|                  Case Type                    |\n"
                + "|_______________________________________________|\n"
                + " \n"
                + " _______________________________________________\n"
                + "|                        |\n"
                + "| 1: Enter 1 for Accident Injury                | \n"
                + "| 2: Enter 2 for Fire Injury                    | \n"
                + "| 3: Enter 3 for Electricity Sho                | \n"
                + "| 4: Enter 4 for Heart Attack                   | \n"
                + "| 5: Enter 5 for Unconscious                    | \n"
                + "| 6: Enter 6 for Otherwise                      | \n"
                + "|_______________________________________________|\n"
                + " > Please enter your choice: \n"
                + " ");
int choice = input.nextInt();

if (int==1)

{
// index patient at 0 

}
7
  • 1
    Why don't use an Object Patinet where it have name lastname and ID? Commented Jan 5, 2017 at 21:31
  • I'd not suggest to use the patients' ID as an index of the array. Rather use the ID as just a number to identify the patient. You only get a problem when there are more than one patient with the same severity. Commented Jan 5, 2017 at 21:34
  • Think object oriented, like @Gatusko said: make it an object with all the user provided data and the ID can be a sequence number generated by your program (usually te sequence number is generated by the database). Commented Jan 5, 2017 at 21:34
  • @Gatusko How do i do that? Commented Jan 5, 2017 at 21:34
  • Oh, and do not use arrays, but rather lists, which can grow to any size without an IndexOutOfArrayBoundaryException. Commented Jan 5, 2017 at 21:36

3 Answers 3

1

Well if you cannot use Objects then you use one For Everything

   for (int i = 0; i < ID.length; i++) {
         System.out.print("\n       > Enter patient ID: ");
         ID[i] = input.nextInt();

        System.out.print("\n         > Enter patient First Name: ");
        first[i] = reader.nextLine();

        System.out.print("\n        > Enter patient last Name: ");
        last[i] = reader.nextLine();
                    }

Now when someone search by ID you only need the search the index of where is it saved and is the same as the name and lastName. Hope if it help you

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

2 Comments

will this work if the user has to enter the ID, first and last names AND THEN the case severity options are shown?
You need to test it and see if it's working because if you put 5 Patients. Then it will show 5 times this. Enter Id, (your input), Enter name (your input), Enter LastName (your input). Then it will show your Options! Only if you inserted correctly all the inputs because there is not a validation. I don't know if you can use try and catch
0

To give you some code regarding the comments on your post. The class you need to store patient information can look like that:

public class Patient
{
  private int id;
  private String name;
  private String lastname;

  public Patient (int id, String name, String lastname)
  {
    // assign values to variables here
  }

  // generate getters and setters
}

Then in your programm you can use the list like:

List<Patient> patients = new ArrayList<Patient>();
Patient p = new Patient (1, "Name", "Lastname");
patients.add(p);

Edit: As you are only allowed to use Arrays use this in you main:

Patient [] patient = new Patient[2];

Patient one = new Patient(1, "foo", "bar");
Patient two = new Patient(2, "foo2", "bar2");

patient[0] = one;
patient[1] = two;

for (Patient pat : patient) {
    System.out.println("Patient " + pat.getId() + ": " + pat.getName() + " " + pat.getLastname() );
}

The Patient class stays the same. After entering you only need to sort by the patients severity which can also be added to Patient class.

Comments

0

Use multi diamentional arrays as follows

int[][] severity = new int[ArraySize][2]; // this array stors ID,severity_code respectively
for (int i = 0; i < first.length; i++) {
 System.out.print("\n _______________________________________________\n"
                + "|                                               |\n"
                + "|                  Case Type                    |\n"
                + "|_______________________________________________|\n"
                + " \n"
                + " _______________________________________________\n"
                + "|                        |\n"
                + "| 1: Enter 1 for Accident Injury                | \n"
                + "| 2: Enter 2 for Fire Injury                    | \n"
                + "| 3: Enter 3 for Electricity Sho                | \n"
                + "| 4: Enter 4 for Heart Attack                   | \n"
                + "| 5: Enter 5 for Unconscious                    | \n"
                + "| 6: Enter 6 for Otherwise                      | \n"
                + "|_______________________________________________|\n"
                + " > Please enter your choice: \n"
                + " ");
severity[i][0] = ID[i]; // storing ID
severity[i][1] = input.nextInt(); // storing severity_code
}

// now you have associated the user's ID with his severity_code
// Then sort severity[][] according to the severity_code which is in the severity[i][1] where 0<=i<=ID.length

Comments

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.