Integer.parseInt出錯,爲什麽?

maoliao 2002-08-13 09:39:16
class TestInteger
{
public static void main(String[] args) throws java.io.IOException
{

System.out.println("Please enter a integer");
String name="";
int c;
while((c=System.in.read())!='\n')
{
name+=(char)c;
}

System.out.println("name="+Integer.parseInt(name1));
}
}

//error!
//Exception in thread "main" java.lang.NumberFormatException q00
at java.lang.Integer.parseInt(Integer.java:426)
at ...
...全文
68 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
maoliao 2002-08-13
  • 打赏
  • 举报
回复
binriyue(日月) is right,thank you!
wujinbao 2002-08-13
  • 打赏
  • 举报
回复
最好用System.out.println("name="+Integer.parseInt(name,10));
maoliao 2002-08-13
  • 打赏
  • 举报
回复
Sorry,name1是笔误
dreamingmouse 2002-08-13
  • 打赏
  • 举报
回复
把这个程序运行一下看看 我觉得你需要捕获一下异常!!
class TestInteger
{
public static void main(String[] args) throws java.io.IOException
{
try{
System.out.println("Please enter a integer");
String name="";
int c;
while((c=System.in.read())!='\n')
{
name+=(char)c;
}

System.out.println("name="+Integer.parseInt(name));
}catch(Exception e){
System.out.println("input a number please!!");
}
}
}
binriyue 2002-08-13
  • 打赏
  • 举报
回复
System.out.println("name="+Integer.parseInt(name.trim()));
zmrljl 2002-08-13
  • 打赏
  • 举报
回复
你是不是输入的字符串了
Reve 2002-08-13
  • 打赏
  • 举报
回复
Attention:
"name1" --> "name"


class TestInteger
{
public static void main(String[] args) throws java.io.IOException
{

System.out.println("Please enter a integer");
String name="";
int c;
while((c=System.in.read())!='\n')
{
name+=(char)c;
}

System.out.println("name="+Integer.parseInt(name));
}
}

62,616

社区成员

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

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