简体   繁体   中英

constructor Service.Service() is not applicable

I have a class with objects

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;
    }

and I want add to listArray the data that they are in excel file

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) {

So they tell me this error:

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

How can I resolve this problem?

thank you

Use Integer.parseInt(String) as this:

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

Hope it helps :)

I assume your getContents return a String object. And your constructor expect an "Integer" object. Search the forum StackOverFlow itself, or look at API Integer.valueOf(String s) to convert String to Integer.

You should change the code to

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

Apparently, the cell content is returned as a String. If you know for sure you'll find an integer in there, you can parse it out of that String like this:

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

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