'incompatible types: cell to String any idea how to resolve the problem?

I want to read the content of an Excel's cell and insert it in an array list but I get the following error: incompatible types, cell cannot be converted to String how to fix the problem plz?!!

a part of the code where the error exists:

while (rowIterator.hasNext()) {
    Row row = rowIterator.next();

    // Now let's iterate over the columns of the current row
    Iterator<Cell> cellIterator = row.cellIterator();
    int j=0;
    while (cellIterator.hasNext()) {

        Cell cell = cellIterator.next();
        Double cellValue;
        cellValue = Double.parseDouble(cell);
        dataPoint.add(cellValue);
        System.out.print(cellValue + "\t");
    }


Solution 1:[1]

Double#parseDouble takes an String argument but you are trying to pass Cell object to it.

You can get the cell value using cell.getStringCellValue();. So, your code should looks like below :

cellValue = Double.parseDouble(cell.getStringCellValue());

If you face any issue to get cell value as String from a numeric cell you can set the cell type to String by calling cell.setCellType(Cell.CELL_TYPE_STRING) before getting the value from the cell.

cell.setCellType(Cell.CELL_TYPE_STRING);
cellValue = Double.parseDouble(cell.getStringCellValue());

Edit:

However, it is recommended that we should check the cell type and then get the value of the cell accordingly instead of setting the cell type Cell#setCellType for getting the value. For more info please visit the link.

Solution 2:[2]

the new code where I tried to check the cell's type first!!

  for (Row row : sheet1) {
        for (Cell cell : row) {


        // Alternatively, get the value and format it yourself
        switch (cell.getCellType()) {
            case CellType.NUMERIC:
                cellValue=cell.getNumericCellValue();
                break;

            default:
                CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                System.out.println("The cell"+cellRef.formatAsString()+"Does not contain numeric value");
        }
        dataPoint.add(cellValue);
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 001