2 Replies Latest reply: Aug 2, 2010 1:18 PM by 800387 RSS

    Convert long String with Integers to Long throwing a NumberFormatException

    843810
      I am working working on extracting some excel data and for the most part the snippet I have below works fine and write to the database columns with out any issue. But I just started to get Number Format Exception when I extract the excel sheet with bit longer String that contains integers. I thought java Long primitive has 8 bit capacity. So I am trying to figure what I am doing wrong. It will be great if I could get any feed back on this.

      FYI: I am using JavaExcel API, and I know it has nothing to do with this error because so far I have imported close 15 excel spread sheets and only different between those file and this file is that the String that is getting converted has more integers in it.

      Exception:
      Exception in thread "main" java.lang.NumberFormatException: For input string: "525335710301092000000"
           at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
           at java.lang.Long.parseLong(Long.java:415)
           at java.lang.Long.parseLong(Long.java:461)
           at com.cannontech.db.ExcelFileDigester.extractColumnData(ExcelFileDigester.java:144)
           at com.cannontech.db.ExcelFileDigester.readExcelSheet(ExcelFileDigester.java:84)
           at com.cannontech.db.ExcelFileDigester.main(ExcelFileDigester.java:55)
      Code Snippet:
                //Getting ready to grab Board serial number.
                LabelCell boardSerialLabel = sheet.findLabelCell("Board Serial #");
                Long[] convertBoardSerialNumber = null;
                Cell[] boardSerialNumColumn = null;
                if(boardSerialLabel != null) {
                     int columnBoardSerial = sheet.findLabelCell("Board Serial #").getColumn();
                      boardSerialNumColumn = sheet.getColumn(columnBoardSerial);
                     //board serial number is a long integer.
                     convertBoardSerialNumber = new  Long[serialNumColumn.length];
                     Cell thirdCellValue = sheet.getCell(columnBoardSerial, 0);
      
                     //Avoiding the first cell value.
                     if(boardSerialLabel.equals(thirdCellValue)) {
                          for(int i =0; i < boardSerialNumColumn.length; i++) {
                               String bSerialNumber = boardSerialNumColumn.getContents().trim();
                               if(!boardSerialNumColumn[i].equals(boardSerialLabel)) {
                                    Long number = Long.parseLong(bSerialNumber);
                                    convertBoardSerialNumber[i] = number;
                               }
                          }
                     }
                }
      Thank You,