i cant figure out how i can make a method which returns an (updated) ArrayList. I want a method which asks for user input, make an object out of the input, and than put it in a list. I want the method to return this list to the main.
This is what i made in the main method:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Doctor> listOfDoctors = new ArrayList<>();
ArrayList<Examination> listOfExaminations = new ArrayList<>();
ArrayList<Patient> listOfPatients = new ArrayList<>();
while (true) {
menu(); //shows options 1 to 5
String option = scanner.nextLine();
if (option.isEmpty()) {
break;
}
if (option.equals("1")) {
System.out.println("What is the name of the doctor?");
String nameDoctor = scanner.nextLine();
System.out.println("What is the doctor's specialisation?");
String specialisationDoctor = scanner.nextLine();
listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
}
I want to make a method of option 1 but i dont know how. This is what i tried:
public static ArrayList<> addDoctorToList(ArrayList<>) {
ArrayList<Doctor> listOfDoctors = new Arraylist<>();
Scanner scanner = new Scanner(System.in);
System.out.println("What is the name of the doctor?");
String nameDoctor = scanner.nextLine();
System.out.println("What is the doctor's specialisation?");
String specialisationDoctor = scanner.nextLine();
listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
return listOfDoctors;
I hope someone could help me.