简体   繁体   English

Java找不到合适的构造函数

[英]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. 对于我的任务,我创建了一个名为Agency的类,它将存储有关人才代理的数据,并且我创建了所需的属性。 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. 我可以使用getter和setter来显示我想要的输出但是现在我想创建对象Agency的新实例并添加它们的详细信息但是我得到一条错误消息,说没有找到合适的构造函数。 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. 注释掉的行可以工作,但如果我尝试从'Agency a = new Agency()'行完成所有操作,它将无效。

plz help am a novice and am probably using bad terminology so not sure if it makes sense what im trying to do. PLZ帮助我是一个新手,我可能使用了不好的术语,所以不确定它是否有意义我想尝试做什么。

Ok added a new constructor which takes it as one string but get an error with integer value for phonenumber. 确定添加了一个新的构造函数,它将它作为一个字符串,但获得了一个带有整数值的错误,用于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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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