求解:数据类型数组与字符串数组的转换

聖炁丗 2011-10-13 01:08:48
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,可是我改了几次还是没成功,很郁闷,所以请大家受累给指导一下!谢了!
...全文
59 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
聖炁丗 2011-10-14
  • 打赏
  • 举报
回复
谢谢大家了 !长见识了!
qybao 2011-10-13
  • 打赏
  • 举报
回复
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"
//这样的字符串怎么能转换为数值类型?需要再按,分割
}
//后面就不解释了
softroad 2011-10-13
  • 打赏
  • 举报
回复
先处理成纯数字
softroad 2011-10-13
  • 打赏
  • 举报
回复
1,8,5,;4,7,9,;2,3,6
Acesidonu 2011-10-13
  • 打赏
  • 举报
回复
d[t]不是纯数字怎么转啊

23,409

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧