简体   繁体   中英

Java no suitable constructor found

For my assignment I have created a class called Agency which will store data about talent agencies and i have created the required attributes. I've also been given a data file which i will need to read from. But first i am to test my class. I can use the getter and setters fine to display my desired output but now I want to create new instance of object Agency and add the details their but i get an error message saying no suitable constructor found. I have matched the settings to my constructor or at least can't see where my mistake is. Any help thanks.

public class Agency {

    //Attributes for class Agency

    private String name;
    private String[] address;
    private String[] adminStaff;
    private int phoneNumber;
    private String type;

    /**
     *Constructor that creates object Agency 
     */
    public Agency() {

    }

    /**
     * Constructor that creates object Agency with values
     * @param name
     * @param address
     * @param adminStaff
     * @param phoneNumber
     * @param type 
     */
    public Agency(String name, String[] address, String[] adminStaff, int phoneNumber, String type) {
        this.name = name;
        this.address = address;
        this.adminStaff = adminStaff;
        this.phoneNumber = phoneNumber;
        this.type = type;
    }

    /**
     * 
     * @return 
     */
    public String getName() {
        return name;
    }

    /**
     * 
     * @param name 
     */
    public void setName(String name) {
        this.name = name;
    }


    /**
     * 
     * @return 
     */
    public String[] getAddress() {
        return address;
    }

    /**
     * 
     * @return 
     */
    public String[] getAdminStaff() {
        return adminStaff;
    }

    /**
     * 
     * @return 
     */
    public int getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * 
     * @param phoneNumber 
     */
    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * 
     * @return 
     */
    public String getType() {
        return type;
    }

    /**
     * 
     * @param type 
     */
    public void setType(String type) {
        this.type = type;
    }  

    private String getArrayAsString(String[] a) {
        String result = "";
        /*for(int i = 0; i<a.length; i++)
        {
            result += a[i] +" ";
        }*/
        for(String s: a)
        {
            result += s + " ";
        }
        result = result.trim();
        return result;
    }

    private String[] getStringAsArray(String a) {
        String[] result = a.split(":");
        return result;
    }

    public void setAddress(String Address) {
        this.address = getStringAsArray(Address);
    }

    public void setAddress(String[] Address) {
        this.address = Address;
    }

    public String getAddressAsString() {
        return getArrayAsString(address);
    }

    public void setAdminStaff(String adminStaff) {
        this.adminStaff = getStringAsArray(adminStaff);
    }

    public void setAdminStaff(String[] adminStaff) {
        this.adminStaff = adminStaff;
    }

    public String getAdminStaffAsString() {
        return getArrayAsString(adminStaff);
    }

    @Override
    public String toString(){
            return name +"\t"+ getAddressAsString() +"\t"+ 
                   getAdminStaffAsString() +"\t"+ phoneNumber +"\t"+ type + "\n";
    }
}

My main Class

public class AgencyTest {

public static void main(String[] args) {    
    /*a.setName("Potters Talent Agency");
    a.setAddress("126-182 Ashwood Road:Potters Bar:Hertfordshire EN6 2:UK");
    a.setAdminStaff("Megan Speagle:Daron Spilman");
    a.setPhoneNumber(2598052);
    a.setType("All");
    System.out.println(a.getName());
    System.out.println(a.getAddressAsString());
    System.out.println(a.getAdminStaffAsString());
    System.out.println(a.getPhoneNumber());
    System.out.println(a.getType());*/
     Agency a = new Agency("Potters Talent Agency, 126-182 Ashwood Rd: Potters Bar: Hertfordshire EN6 2: UK, "
             + "Megan Speegle:Daron Spilman:Leonel Striegel:Hubert Tesoro:Nathanial Pompey, 2598052, All");
    System.out.println(a);
}

} Commented out lines work but if i try to do it all from the 'Agency a = new Agency()' line it won't work.

plz help am a novice and am probably using bad terminology so not sure if it makes sense what im trying to do.

Ok added a new constructor which takes it as one string but get an error with integer value for phonenumber. Here's the code:

`public Agency(String csvString) {
        String[] attributes =csvString.split(",");
        int i = 0;
        for(String s : attributes)
        {
            switch(i)
            {
                case 0:
                    this.name = s;
                    break;
                case 1:
                    this.address = getStringAsArray(s);
                    break;
                case 2:
                    this.adminStaff = getStringAsArray(s);
                    break;
                case 3:
                    this.phoneNumber = getIntAsString(s);
                    break;
                case 4:
                    this.type = s;
                    break;
            }

I have tried to convert it to string but doesn't seem to work. More Code:

 private String getIntAsString(int a){
    String result = Integer.toString(a);
    return result;
}

public String getPhoneNumberAsString() {
    return getIntAsString(phoneNumber);
}
public void setPhoneNumber(int phoneNumber){
    this.phoneNumber = phoneNumber;
}
        i++;
    }`
Agency a = new Agency("Potters Talent Agency ... EN6 2: UK, "
         + "Megan Speegle:Daron ... 2598052, All");

is trying to call a constructor with a single string and no such constructor exists.

You need to either provide such a constructor (which will split the string into its component fields), or split the string yourself and call the current constructor with those component fields.

In the line

Agency a = new Agency("Potters [...] All");

you are basically calling a constructor that takes a single string as its argument. So the compiler is looking for such a constructor:

Agency(String properties) { ... }

This constructor does not exist. Instead you have a constructor

Agency(String name, String[] address, String[] adminStaff, int phoneNumber, String type) { ... }

which takes the arguments separately. So you must call it like so:

Agency a = new Agency(
        "Potters Talent Agency",
        new String[] {"126-182 Ashwood Rd", "Potters Bar", "Hertfordshire EN6 2", "UK"},
        new String[] {"Megan Speegle", "Daron Spilman", "Leonel Striegel", "Hubert Tesoro", "Nathanial Pompey"},
        2598052,
        "All"
);

You could - alternatively - provide a constructor taking a single string, which then does some string magic to disassemble the parts. This is somewhat complex and error-prone, so I would advise not to do so.

See error is in

 Agency a = new Agency("Potters Talent Agency, 126-182 Ashwood Rd: Potters Bar: Hertfordshire EN6 2: UK, "
             + "Megan Speegle:Daron Spilman:Leonel Striegel:Hubert Tesoro:Nathanial Pompey, 2598052, All");

You are simply passing one string to the constructor and there is no constructor in your class which accepts the only string as parameter. So form your parameters properly.

Try to create object this way

String myAddress[] = "BLR India".split(" ");
    String myAdmin[] = "my admin array".split(" ");
    Agency a = new Agency("MyName", myAddress, myAdmin, 1235678, "myType");

You are using wrong constructor. What you have now is

Agency a = new Agency("Potters Talent Agency, 126-182 Ashwood Rd: Potters Bar: Hertfordshire EN6 2: UK, "
             + "Megan Speegle:Daron Spilman:Leonel Striegel:Hubert Tesoro:Nathanial Pompey, 2598052, All"); 

So it will expect a constructor with just single string argument which you don't have.

You should do something like

  List<String> addr = new ArrayList<String>();
    addr.add("126-182 Ashwood Rd");
    addr.add("Potters bar");
    addr.add("Hertfordshire EN6 2");
    addr.add("UK");
    List<String> adminStaff = new ArrayList<String>();
    adminStaff.add("Megan Speegle");
    adminStaff.add("Daron Spilman");
    agency a = new Agency("Potters Talent Agency", addr.toArray(), adminStaff.toArray(),2598052, "All");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM