List <pdfpcell>不适用于参数(String)</pdfpcell>

时间:2013-09-10 11:20:03

标签: java string arraylist arguments

我设置了字符串,我需要将它们添加到 ArrayList 中,类型为 PdfPCell ,以便稍后处理它们iText图书馆。这是代码:

try {
    Scanner scan = new Scanner(new File("file.txt"));
    scan.useDelimiter(",|" + System.getProperty("line.separator"));

    while(scan.hasNext()) {
        String id = scan.next();
        String txt1 = scan.next();
        String txt2 = scan.next();
        String txt3 = scan.next();

        // ArrayList with PdfPCell type
        List<PdfPCell> allCols = new ArrayList<PdfPCell>();
        allCols.add(id);
        allCols.add(txt1);
        allCols.add(txt2);
        allCols.add(txt3);

        System.out.println(allCols);
    }
    scan.close();
} catch (Exception e) {
    e.printStackTrace();
}

错误: The method add(PdfPCell) in the type List<PdfPCell> is not applicable for the arguments (String)

我被困在这里。如何解决这个问题呢?提前谢谢。

2 个答案:

答案 0 :(得分:0)

列表allColsPdfPCell输入。您无法将String对象添加到其中。由于String不是PdfPCell :)的子类,所以它应该引发编译时错误。

你应该简单地创建PdfPCell添加到其中的对象。

List<PdfPCell> allCols = new ArrayList<PdfPCell>();
allCols.add(new PdfPCell(new Phrase(id));
allCols.add(new PdfPCell(new Phrase(txt1));
...

答案 1 :(得分:0)

idString,无法直接转换/类型转换为自定义类型PdfPCell。即使PdfPCell只有1个String实例变量。

要解决此问题,您可以将 String参数构造函数添加到PdfPCell或更好地添加创建工厂。

List<PdfPCell> allCols = new ArrayList<PdfPCell>();
allCols.add(Factory.getPdfCell(id));//or
allCols.add(new PdfPCell(id));
相关问题