【发布时间】:2014-06-21 18:50:10
【问题描述】:
这是我的方法,但是当我尝试为负数运行它时,我得到一个 NumberFormatException 输入“-”。
public newObj(String s)
{
list = new LinkedList<Integer>();
String[] splitted = s.split("\\d");
int[] ints = new int[splitted.length];
for (int i = 0; i < splitted.length - 1; i++) {
ints[i] = Integer.parseInt(splitted[i]);
}
for (int j = 0; j < ints.length - 1; j++) {
list.add(ints[j]);
}
}
我的输入字符串只是一个数字,如“-123456”或“12345”。正数工作,但我不能让负数工作。
对于我的否定输入字符串,我希望我的列表类似于 [-1,-2,-3,-4,-5,-6]。
【问题讨论】:
-
(per Ren8888) "你的输入字符串是什么?"
标签: java string parsing integer