-1

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.

4 Answers4

0

There's 2 different things you can do :

  1. 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<Doctor> addDoctorToList(ArrayList<Doctor> listOfDoctors ) {
    Scanner scanner = new Scanner(System.in);
...
  1. 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<Doctor> addDoctorToList() {
    ArrayList<Doctor> listOfDoctors = new Arraylist<>();
    Scanner scanner = new Scanner(System.in);
...
Bonana
  • 520
  • 4
  • 9
  • What i want to achieve is the option 1 that you gave me. If i try to do this i get a message which says "illegal start of type", also the "add" in my code is "erroneous". I have no clue about what im doing wrong. – Quincy van Deursen Oct 05 '21 at 15:49
  • `"illegal start of type"` means you have a syntax error. There's a typo somewhere. Try it again, and if it doesn't work post the *entire code* here and someone will look at it. – markspace Oct 05 '21 at 15:56
  • @markspace I edited with instead of <> for params & return types. Are yo using an IDE (Eclipse, IntelliJ? it'll help you find these typos) – Bonana Oct 06 '21 at 10:00
0

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
Chukwu Remijius
  • 323
  • 1
  • 14
  • Im not sure if im following you. What i want that the methods does is creating and adding a doctor object to a list (which exists in the main). Ive tried your approach, but i changed the parameter ArrayList to ArrayList (since i want to pass and update an exsisting arraylist containing Doctor obejcts. The problem right now is that the list in the main doesnt get 'updated'. The added Doctor object in the method isnt added to the ArrayList in the main. – Quincy van Deursen Oct 05 '21 at 16:48
  • Your doctor array list can only take a doctor object as its value because it's a type of Doctor object. If your adding any other value it wont update – Chukwu Remijius Oct 05 '21 at 18:53
  • That means your Doctor class suppose to return a type of doctor object or return itself – Chukwu Remijius Oct 05 '21 at 18:55
  • Your problem can be java typeof error. Objects in java has to be a typeof another object in relationship – Chukwu Remijius Oct 05 '21 at 18:56
  • Check this answer https://stackoverflow.com/questions/61104708/typeof-in-java-8 – Chukwu Remijius Oct 05 '21 at 18:59
0

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?

  • Thanks for your effort, but your code doesnt return anything. It doesnt really add something to the listOfDoctors in the main. What i want is that the existing list in the main gets updated when someones adds a doctor through the method. – Quincy van Deursen Oct 05 '21 at 16:52
  • uhm, maybe I didn't get what you need...The method whic add a doctor is created in the main or in an other class? – Alessandro Oct 05 '21 at 17:23
  • the method is created in the main. – Quincy van Deursen Oct 05 '21 at 17:25
  • If the list and the method are in the same class my method should add a new doctor at the list and you don't need to return anything but, if you need to manage the list, just call up the list and use the others methods... Am I completely wrong? – Alessandro Oct 05 '21 at 17:37
0

You were close. Your code should have one and only one System.in Scanner.

You're creating an ArrayList of doctors, which implies more than one doctor. You need to put the Scanner code in a loop so the user can input multiple doctors. The user has to be able to end the loop, so I tested for the user just pressing the Enter key and not typing anything. You can change it to "quit" or "done" if you wish.

Here's the modified method.

public static ArrayList<> addDoctorToList(Scanner scanner) {
    ArrayList<Doctor> listOfDoctors = new Arraylist<>();
    
    String nameDoctor = "";
    do {
        System.out.println("What is the name of the doctor?");
        nameDoctor = scanner.nextLine().trim();
        if (!nameDoctor.isEmpty()) {
            System.out.println("What is the doctor's specialisation?");
            String specialisationDoctor = scanner.nextLine().trim();
            listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
            System.out.println("added!");
        }
    } while (!nameDoctor.isEmpty());

    return listOfDoctors;
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111