开发者

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String.

开发者 https://www.devze.com 2023-03-29 23:25 出处:网络
This is My code: Workbook workbook = new Workbook(); workbook.open(file开发者_如何转开发name); Worksheet worksheet = workbook.getWorksheets().getSheet(sheetno);

This is My code:

      Workbook workbook = new Workbook();
        workbook.open(file开发者_如何转开发name);
        Worksheet worksheet = workbook.getWorksheets().getSheet(sheetno);
        Cells cells=worksheet.getCells();
        int num=cells.getMaxDataRow();
        int num1=cells.getMaxDataColumn();
        int OCount=1;
                     for (int n1=startpos+1;n1<endpos;n1++)
         {   if (cells.checkCell(n1, Colno).getValue()==null )
                       {         Cell cell=cells.getCell(n1,Colno);
                                Style style = cells.getCell( n1,Colno).getStyle();
                                style.setColor(Color.TEAL);
                                cell.setStyle(style);
                       } else if(cells.checkCell(n1, Colno).getValue().toString().length()==0) { Cell cell=cells.getCell(n1,Colno);
                                Style style = cells.getCell( n1,Colno).getStyle();
                                style.setColor(Color.TEAL);
                                cell.setStyle(style); } else{ double intCounter = Double.parseDouble(cells.checkCell(n1,Colno).getValue().toString());
                          System.out.println(cells.checkCell(n1,Colno).getValue().toString());
                          if(intCounter!=Count){
                                       Cell cell=cells.getCell(n1,Colno);
                                        Style style = cells.getCell( n1,Colno).getStyle();
                                        style.setColor(Color.YELLOW);
                                        cell.setStyle(style);
                                  }
                              }
                  Count=Count+1;

             } workbook.save("C:\\output.xls",FileFormatType.EXCEL97TO2003);                                                                                   

I am trying to check that Sr no is in sequential order or not. it is working fine if there is no empty string " ". For empty string it throws

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String.

Thanks in Advance....


The problem is this line of code:

Double.parseDouble(cells.checkCell(n1,Colno).getValue().toString());

There you try to make a Double out of an empty String. Check the documentation and you will see that the NumberFormatException is the intended behavior. So you either have to check first if the String is empty or implement proper error handling.

Here a quote from the API for the method Double.parseDouble(...):

Throws: NumberFormatException - if the string does not contain a parsable double.


its better to handle these cases by using a utility method.

private Double getCorrectedDouble(CellValues... ){
  //check and handle empty fields
  //return new Double(0) if the target is empty 

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号