user905527
user905527

Reputation: 277

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

This is My code:

      Workbook workbook = new Workbook();
        workbook.open(filename);
        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....

Upvotes: 1

Views: 3841

Answers (2)

gtiwari333
gtiwari333

Reputation: 25184

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 

}

Upvotes: 0

RoflcoptrException
RoflcoptrException

Reputation: 52247

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.

Upvotes: 3

Related Questions