求解:数据类型数组与字符串数组的转换
public class SelectSort {
public static void main(String args[]) {
String s = "1,8,5,;4,7,9,;2,3,6";
String d[] = s.split(";");
int t;
int a[] = new int[d.length];
for (t = 0; t < d.length; t++) {
try {
a[t] = Integer.parseInt(d[t]);
} catch (NumberFormatException e) {
System.out.println("error");
}
for (t = 0; t < d.length; t++) {
System.out.print(a[t] + " ");
}
selectSoft(a);
System.out.println("现在数据 = " + Arrays.toString(a));
}
}
private static void selectSoft(int a[]) {
int i, j, k, temp;
for (i = 0; i <= a.length; i++) {
k = i;
for (j = i + 1; j < a.length; j++) {
if (a[k] > a[j]) {
k = j;
}
}
if (k != i) {
temp = a;
a = a[k];
a[k] = temp;
}
}
}
}
这段代码是我心血来潮自己编的,可是编译时“ a[t] = Integer.parseInt(d[t]);”这段代码总是报错,我查了一下API文档发现是报错的原因是if the string does not contain a parsable integer,可是我改了几次还是没成功,很郁闷,所以请大家受累给指导一下!谢了!
[解决办法]
d[t]不是纯数字怎么转啊
[解决办法]
1,8,5,;4,7,9,;2,3,6
[解决办法]
先处理成纯数字
[解决办法]
public class SelectSort { public static void main(String args[]) { String s = "1,8,5,;4,7,9,;2,3,6"; String d[] = s.split(";"); //d[0] = "1,8,5", d[1]="4,7,9" //这样的字符串怎么能转换为数值类型?需要再按,分割 } //后面就不解释了