字符串转int类型 怎么转不了

mr_qiu_cc 2012-05-29 03:26:47
String tmp = ReadContentTXT(context, "txt.txt");
int ci = 0;
char tp;
char num[] = new char[10];
String to;
for (int j = 0; j < tmp.length(); j++) {
tp = tmp.charAt(j);

if(tp != '\r' && tp != '\n')
{
switch(tp)
{
case ',':
ci = 0;
to = String.valueOf(num);
to.trim();
Integer.parseInt(to);
System.out.println(to);
Arrays.fill(num, ' ');
break;
case '|':
System.out.print("faf");
break;
case ';':
break;
default:
num[ci] = tp;
ci++;
break;
}
}

}
...全文
177 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdojqy1122 2012-05-29
  • 打赏
  • 举报
回复
单从上面看我没看见你赋值了。其次你的trim方法没有起作用。
String to = String.valueOf(num);
System.out.println("|" + to + "|");
int data = -1;
if(!"".equals(to = to.trim())){
data = Integer.parseInt(to.trim());
}

换上这段代码应该能出结果。
huage 2012-05-29
  • 打赏
  • 举报
回复
强大的try{}catch(){}在用吗,其实我想说的带代码里只要有强转还有一些不确定的时候都try一下,习惯
ladybirds2008 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
case ',':
ci = 0;
to = String.valueOf(num);
to.trim();
Integer.parseInt(to);
System.out.println(to);
没有看到 你的num在哪?只看到num[]
不知道 你怎么会有这种错。。。怎么会走到 Integer.parseInt(to);才报异常 而且是Source not found.
……
[/Quote]原来num[]是char啊。。。。误会
ladybirds2008 2012-05-29
  • 打赏
  • 举报
回复
case ',':
ci = 0;
to = String.valueOf(num);
to.trim();
Integer.parseInt(to);
System.out.println(to);
没有看到 你的num在哪?只看到num[]
不知道 你怎么会有这种错。。。怎么会走到 Integer.parseInt(to);才报异常 而且是Source not found.
我从没遇到过。。
mr_qiu_cc 2012-05-29
  • 打赏
  • 举报
回复
我打印了,to有值, 也打印了to的长度,是10, 因为我定义 char数组给的是10位,
我自己定义一个 String ss = "111"; 转起来没问题, 可能就是 char 定义的问题
龙四 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

断点,走到 Integer.parseInt(to);
就报错 Source not found. 说找到资源,晕死啊
[/Quote]


这不是错误,不影响执行
商科程序员 2012-05-29
  • 打赏
  • 举报
回复
把to打印出来看看,是不是空字符串或非数字?
非数字该报NumberFormatException
mr_qiu_cc 2012-05-29
  • 打赏
  • 举报
回复
断点,走到 Integer.parseInt(to);
就报错 Source not found. 说找到资源,晕死啊

50,539

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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