0

i have done following to retrieve code from xls sheet and csv file. i tried using ".trim()" but I am not getting the required output. Please tell where I am going wrong in order to remove any trailing blank spaces from xls cell and csv file.

 for (int rowNum = rowStart-1; rowNum < rowEnd+1; rowNum++) {
            List<String> cellTempList = new ArrayList<String>();
            Row rowObj = hssfSheet.getRow(rowNum);


            for (int col = 0; col < columnEnd; col++) {
                Cell cell = rowObj.getCell(col, Row.CREATE_NULL_AS_BLANK);
                if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)
                {

                    DecimalFormat df = new DecimalFormat("#.##");
                    String cellValue=df.format(cell.getNumericCellValue());
                    cellTempList.add(cellValue.toString());
                    logger.trace(TId + " Adding cell having string to "+cellTempList);
                } else {
                    cellTempList.add(cell.toString()+"");
                }
            }

            cellDataList.add(cellTempList);


 //following to retrieve data from a csv file 
  while ((line = stream.readLine()) != null) {
            if(iteration < iteration1-1 || StringUtils.isBlank(line)) {
                iteration++;  
                continue;
            }

            //String[] splitted = line.split(",");
            String[] splitted = StringUtils.splitPreserveAllTokens(line,',');
            List<String> dataLine = new ArrayList<String>(splitted.length);
            for (String data : splitted)
                dataLine.add(data.trim());
            csvData.add(dataLine);

        }
    }
    catch (Exception e) {
        logger.error(TId + " Other Exception : " + e.getStackTrace());
        throw new Exception("-8");
    }
    finally {
        if (stream != null)
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                logger.error(TId + " IOException : " + e.getStackTrace());
                throw new Exception("-8");
            }
    }
    logger.info(TId + " [Flow]:Exiting readcsvFile Method...." + csvData);
    return csvData;

}

please help i tried trim but i am not getting the required output

akash
  • 105
  • 2
  • 13
  • If u get the value in string then simply use trim() method.What issue u r facing ? – Kick Feb 13 '14 at 06:28
  • show _current output_ and _expected output_. – Baby Feb 13 '14 at 06:29
  • like this - cellTempList.add((cell.toString()).trim()+""); or cellTempList.add(cell.toString().trim()+""); – akash Feb 13 '14 at 06:34
  • 1
    @akash you don't need to `trim` `cell.toString()` its already _trimmed_ . you need to `trim()` each `elements` inside the `list` – Baby Feb 13 '14 at 06:37

0 Answers0