读取txt写入Excel报java.lang.NumberFormatException: For input string: " 17"。

希望可以详聊。
while ((lineTxt = bufferedReader.readLine()) != null) {
Object[] ex = lineTxt.split("-");
Label label = new Label(Integer.parseInt(ex[0].toString()),Integer.parseInt(ex[1].toString()),ex[2].toString());
ws.addCell(label);
}

就是格式转换异常,非int类型强转成int
写入的有字符串呗,要不就是有空格trim()一下,追问

17-1-2014_04_29 15:39:21:420
18-1-时差:11100
17-2-2014_04_29 15:39:32:892
18-2-时差:1610

这个是txt中的数据。

追答

Label()
构造一个空标签。
Label(String text)
使用指定的文本字符串构造一个新的标签,其文本对齐方式为左对齐。
Label(String text, int alignment)
构造一个显示指定的文本字符串的新标签,其文本对齐方式为指定的方式。
label初始化不对
Label label = new Label((Integer.parseInt(ex[0].toString()),Integer.parseInt(ex[1].toString()),ex[2].toString())+"");

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-29
数字格式转换出错追问

写入的时候String 然后object读取"-"分开转换成String在转换成int。我不清楚哪里有问题。方便详细介绍下么?

追答

写的是什么,怎么读的。比如写“123-aa”,可以将字符串使用“-”分开,然后“123”可以转换成数字

相似回答