簡體   English   中英

構造函數Service.Service()不適用

[英]constructor Service.Service() is not applicable

我有一堂課

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

我想將它們在excel文件中的數據添加到listArray

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)

我該如何解決這個問題?

謝謝

使用Integer.parseInt(String)這樣:

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

希望能幫助到你 :)

我假設您的getContents返回一個String對象。 並且您的構造函數期望一個“整數”對象。 搜索論壇StackOverFlow本身,或查看API Integer.valueOf(String s)以將String轉換為Integer。

您應該將代碼更改為

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

顯然,單元格內容以字符串形式返回。 如果您確定可以在其中找到一個整數,則可以像下面這樣從String中解析出來:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM