HI ,I am new in Java and i have trablle creating array of objects .This are my files,and I need to create array of objects patients
thank you in advance for your help

import java.util.*;
public class MainPatient {

    public static void main(String[] args) {
        int choice=0;
        Scanner input=new Scanner(System.in);
        Patient patientObj=new Patient();
        while(choice!=4){
        System.out.println("1.Add new patient");
        System.out.println("2.Display by priority");
        System.out.println("3.Display by name(sorted by last name)");
        System.out.println("4.Exit");
        System.out.print("Enter your choise: ");
        choice=input.nextInt();
        patientObj.makeChoice(choice);

    }
    }
}

--

import java.util.*;
public class Patient {
Scanner input=new Scanner(System.in);
public String fName;
public String lName;
public int priority;
public Patient(){
       this.fName="";
       this.lName="";
       this.priority=0;
       }
public Patient(String fname,String sname,int priority){
       this.fName=fname;
       this.lName=sname;
       this.priority=priority;
       }

public void setfName(String name){
       this.fName=name;
       }
public String getfName(){

    return fName;
    }
public void setlName(String name){
       this.lName=name;
       }
public String getlName(){

    return lName;
 }
public void setPriority(int priority){
      this.priority= priority;
       }
public int  getPriority(){
    return priority;
}
public void setPatient(){
    System.out.print("Enter First Name: ");
    setfName(input.next());
    System.out.print("Enter Last Name: ");
    setlName(input.next());
    System.out.print("Enter Priority: ");
    setPriority(input.nextInt());

}


 public void makeChoice(int choice){
    switch(choice)
       {
        case 1:setPatient();

        break;
        case 2:System.out.println("Display priority");
        break;
        case 3:System.out.println("display by name");
        break;
        case 4:System.out.println("exit");
        break;
        default : System.out.println("wrong choice");
        break;
    }
    }

}

Edited 3 Years Ago by pyTony: Fixed formating

import java.util.ArrayList;
import java.util.Scanner;


public class PatientDirectory {

	private Scanner in = new Scanner(System.in);
	private ArrayList<Patient> directory = new ArrayList<Patient>();
	
	public PatientDirectory() {
		displayMainMenu();
	}
	
	private void displayMainMenu() {
		int choice;
		
		do {
			System.out.println("1.Add new patient");
			System.out.println("2.Display by priority");
			System.out.println("3.Display by name(sorted by last name)");
			System.out.println("4.Exit");
			System.out.println();
			System.out.print("Enter your choise: ");
			choice = in.nextInt();
			
			switch (choice) { 
			case 1: addPatient(); break;
			case 2: displayPriority(); break;
			case 3: displaySortByLastName(); break;
			case 4: break;
			default: continue;
			}
		} while (choice != 4);
		
		System.out.println();
		System.out.println("Good-Bye");
	}
	
	private void addPatient() {
	    System.out.print("Enter First Name: ");
	    String firstName = in.next();
	    
	    System.out.print("Enter Last Name: ");
	    String lastName = in.next();
	    
	    System.out.print("Enter Priority: ");
	    int priority = in.nextInt();
	    
	    directory.add(new Patient(firstName, lastName, priority));
	    
	    System.out.println();
	}
	
	private void displayPriority() {
		// your code
	}
	
	private void displaySortByLastName() {
		// your code
	}
	
	public static void main(String[] args) {
		new PatientDirectory();
	}
}
public class Patient {

	private String firstName;
	private String lastName;
	private int priority;

	public Patient(String firstName, String lastName, int priority){
	   this.setFirstName(firstName);
	   this.setLastName(lastName);
	   this.setPriority(priority);
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public int getPriority() {
		return priority;
	}
}

I didn't implement the last 2 methods, but hopefully you see how this works and you can finish it off.

Edited 5 Years Ago by ztini: n/a

Hi ,again ,I have a few more questions how can I print in this array list.As I have two strings and one integer in every element in the array list, what type are they.I try this but it doesn't work.Also for my compare functions ,how can I reach lets say the integer in this element in the array list (priority)
Thanks a lot for the help

public void printList(){
		for(int i=0;i<patientsList.size();i++){
			System.out.printf("%s",patientsList.get(i));
		}
	}

Edited 5 Years Ago by kukuruku: n/a

System.out.printf("%s",patientsList.get(i));

That retrieves the Patient object that is present at location 'i' in the arraylist. You then need to invoke whatever method you want it.

For instance, to print the name:

System.out.printf("%s",patientsList.get(i).getFirstName());

This of course, assumes you created a patient's arrayList like this:

private ArrayList<Patient> patientList= new ArrayList<Patient>();
This article has been dead for over six months. Start a new discussion instead.