一个类型转换的问题 信誉第一,回答正确后马上揭帖!

mind_1220 2004-08-22 02:06:58
在Java中
--------------

String totel;
……
child = (Element) root.getChild("totle"); //用JDOM接受到的。
totel = child.getText(); //这样的totel 是一个字符型变量
我想把它转换为 int 类型
因为我需要
for(int i=0;i<totel;i++){
……
}
---------------------------
我应该怎么办?
谢谢。
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MopeLeo 2004-08-22
  • 打赏
  • 举报
回复
to chn217 and chenmingjsw

当child.gettext()取得的字符串不是数字行的字符串,Integer.valueof() and Integer.parseInt() 方法根本行不通!
chenmingjsw 2004-08-22
  • 打赏
  • 举报
回复
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
MopeLeo 2004-08-22
  • 打赏
  • 举报
回复
可以转换一下,在totel = child.getText();后加上:
int t = (int)totel.charAt(0);
for(int i = 0 ; i < t ; i ++){
......
}
chn217 2004-08-22
  • 打赏
  • 举报
回复
int totel = Integer.parseInt(child.getText());

81,092

社区成员

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

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