I am attempting to use a variable length argument in my constructor, however I get the error messages:
'Incompatible types string cannot be converted to int'
Illegal start of operation, ';' expected
public class Personnel {
private String pname;
private String rank;
private String certs [];
public static int totalPersonnel = 0;
public Personnel(String name, String rank, String... certs) {
this.pname = name;
this.rank = rank;
this.certs = certs;
incrPersonnel();
}
public static void incrPersonnel(){
totalPersonnel++;
}
public static void main(String myArgs[]){
Personnel Alex = new Personnel ("Alex", "CPT", ["none"] );
}
}
If you try to pass an array then the way you are using is not correct instead you have to use new String[]{"none"}
, so your code should look like this :
public static void main(String myArgs[]) {
Personnel Alex = new Personnel("Alex", "CPT", new String[]{"none"});
}
Or you can also use :
public static void main(String myArgs[]) {
Personnel Alex = new Personnel("Alex", "CPT", "val1", "val2", "val3");
//--------------------------------------------[_____________________]
}
But in your case you pass only one value, so you don't have to use new String[]{..}
, you need just to pass it like this :
Personnel Alex = new Personnel("Alex", "CPT", "none");
If you don't want to pass any value, then you don't need to specify it you can just pass the first and second value like :
Personnel Alex = new Personnel("Alex", "CPT");
//------------------------------------------^____no need to pass
It will return empty
for the array