Reputation: 17
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
Upvotes: 1
Views: 4656
Reputation: 1
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;
}
Upvotes: 0
Reputation: 114
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));
}
Upvotes: 0
Reputation: 86
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);
Upvotes: 2
Reputation: 201527
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"));
Upvotes: 3