gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : empty String. 中的异常

转载 作者:行者123 更新时间:2023-11-29 06:10:20 27 4
gpt4 key购买 nike

这是我的代码:

      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);

我正在尝试检查 Sr no 是否按顺序排列。如果没有空字符串“”,它工作正常。对于空字符串,它抛出

线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:空字符串。

提前致谢....

最佳答案

问题是这行代码:

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

您尝试从一个空字符串中创建一个 Double。 Check the documentation您会看到 NumberFormatException 是预期的行为。因此,您要么必须先检查字符串是否为空,要么实现适当的错误处理。

这里是 API 中 Double.parseDouble(...) 方法的引用:

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

关于java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : empty String. 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7145120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com