1

I'm new to Java. I'm having issues passing a List variable that is part of a constructor definition when I created an object of the class

    public class Patient {

    private String patientfirstName;
    private String patientLastName;
    private List<String> allergyList;


     public Patient(String patientfirstName, String patientLastName, 
     List<String> allergyList) {
     this.patientfirstName = patientfirstName;
     this.patientLastName = patientLastName;
     this.allergyList = allergyList;
      }


     Patient patientobj = new Patient("sean","john","allegry1");

gives an error: "constructor "Str,str,str" not defined."

I need help on how to pass the allergy

4 Answers 4

3

Your need a List<String> instead of a single String, Arrays.asList(T...) is probably the easiest solution:

Patient patientobj = new Patient("sean", "john", Arrays.asList("allergy1"));

And if you have more allergies

Patient patientobj = new Patient("sean", "john", 
        Arrays.asList("allergy1", "allergy2"));
Sign up to request clarification or add additional context in comments.

2 Comments

Will importing the Array class (import java.util.Arrays;) in my main class will be a required step? This is my first-week coding in Java
Yes. Or use java.util.Arrays instead of Arrays (import doesn't exist after compilation anyway).
2
public class Patient {

private String patientfirstName;
private String patientLastName;
private List<String> allergyList;


 public Patient(String patientfirstName, String patientLastName, 
 List<String> allergyList) {
 this.patientfirstName = patientfirstName;
 this.patientLastName = patientLastName;
 this.allergyList = allergyList;
  }




 *Patient patientobj = new Patient("sean","john","allegry1");*// this is wrong you have to pass a list not the string. you should do something like this:

 // first create a list and add the value to it
 List<String> list = new ArrayList<>();
 list.add("allergy1");

 // now create a object and pass the list along with other variables
 Patient patientobj = new Patient("sean","john",list);

Comments

0

You also has a solutionis just add one constructor of the class Patient.

public Patient (String patientfirstName,String patientLastName,String allergeyList){
this.patientfirstName  = patientfirstName;
this.patientLastName = patientLastName;\
this.allergeyList = new ArrayList<>( Arrays.asList(allergeyList));
}

Comments

0

In my opinion, you could use Varargs. Thanks to varargs you can put into the parameters how many arguments do you want

public class Patient {

public String patientfirstName;
public String patientLastName;
public List<String> allergyList;



public Patient(String fName,String lName,String...aList) {
    this.patientfirstName = fName;
    this.patientLastName = lName;
    this.allergyList = Arrays.asList(aList);
}


public static void main(String[] args) {

    Patient firstPatient = new Patient("Foo", "Bar", "First Allergy","Second Allergy");

    Patient secondPatient = new Patient("Foo", "Baz", "First Allergy","Second Allergy","Third Allergy","Fourth Allergy");

    Patient ThirdPatient = new Patient("Foo", "Foo", "First Allergy");
}

The parameter "aList" is like an array because varargs is like an array with no specific lenght,the length you choose when you enter the parameters, as you can see

The type of allergyList is by choice.. you can also do this:

In "Patient" attributes:

 public String[] allergyList;

In the costructor:

public Patient(String fName,String lName,String...aList) {
        this.patientfirstName = fName;
        this.patientLastName = lName;
        this.allergyList = allergyList;
    }

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.