构造函数Service.Service()不适用

时间:2013-06-18 15:51:14

标签: java

我有一个带对象的类

public class Service {

    private Integer vlan;
    private String desc;
    private String vrf;
    private String address;
    private String JR;
    public Service() {
    }
    public Service(Integer vlan, String desc, String vrf, String address, String JR) {
        this.vlan = vlan;
        this.desc = desc;
        this.vrf = vrf;
        this.address = address;;
        this.JR = JR;
    }
    public Service(Integer vlan) {
        this.vlan=vlan;
    }

我希望添加到listArray 他们在excel文件中的数据

try {
    Workbook workbook = Workbook.getWorkbook(new File("Taza-BLR.xls"));
    Sheet sheet = workbook.getSheet(0);
    for(int i=1;i<sheet.getRows();i++)
    {
        liste.add(new Service(sheet.getCell(7, i).getContents()));
    }
} catch (IOException ex) {

所以他们告诉我这个错误:

constructor Service.Service(Integer) is not applicable
  (actual argument String cannot be converted to Integer by method invocation conversion)

如何解决此问题?

谢谢

3 个答案:

答案 0 :(得分:0)

使用Integer.parseInt(String)

liste.add(new Service(Integer.parseInt(sheet.getCell(7, i).getContents())));

希望有所帮助:)

答案 1 :(得分:0)

我假设你的getContents返回一个String对象。而你的构造函数期望一个“整数”对象。搜索论坛StackOverFlow本身,或查看API Integer.valueOf(String s)将String转换为Integer。

您应该将代码更改为

 liste.add(new Service(Integer.valueOf(sheet.getCell(7, i).getContents())));

答案 2 :(得分:0)

显然,单元格内容以字符串形式返回。如果你肯定知道你会在那里找到一个整数,你可以用这样的字符串解析它:

String contents = sheet.getCell(7, i).getContents();
try {
    Integer value = Integer.valueOf(contents);
    liste.add(new Service(value));
catch (NumberFormatException ignored) {}
相关问题