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.
CodePudding user response:
There's 2 different things you can do :
- the method takes an existing list, and add a Doctor to it (you can return it or just modify the existing ArrayList).
In this case, you need a name to your param, and don't create a new ArrayList:
public static ArrayList<> addDoctorToList(ArrayList<> listOfDoctors ) {
Scanner scanner = new Scanner(System.in);
...
- The method creates a new empty ArrayList, and don't need to be passed a param, so remove this type in the parenthesis:
public static ArrayList<> addDoctorToList() {
ArrayList<Doctor> listOfDoctors = new Arraylist<>();
Scanner scanner = new Scanner(System.in);
...
CodePudding user response:
Your method is not a type of Doctor list
Since you are returning a type of doctor object as your list type your method suppose to be the same.
Change this codes:
public static ArrayList<> addDoctorToList(ArrayList<String> var) {
ArrayList<Doctor> listOfDoctors = new Arraylist
To:
public static ArrayList<Doctor> addDoctorToList(ArrayList<String> var) {
ArrayList<Doctor> listOfDoctors = new Arraylist
CodePudding user response:
I guess could work a simple method like this below:
public static void insertDoctor(){
System.out.println("What's the name of the doctor?");
String nameDoctor=scanner.nextLine();
System.out.println("What's the doctor's specislisaton?");
String specialisationDoctor=scanner.nextLine();
listOfDoctor.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
}
What do you think about?