Topic: 请教各位高手关于readline方法的NumberFormatException问题

  Print this page

1.请教各位高手关于readline方法的NumberFormatException问题 Copy to clipboard
Posted by: tyrochen
Posted on: 2008-03-17 08:31

源代码如下:
import java.io.*;
class ArraySort
{public static void main(String[] args) throws IOException
{BufferedReader keyin =new BufferedReader(new InputStreamReader(System.in));
int a[],i, j, k, temp; String c;
System.out.println("Input the number of array elements!");
c=keyin.readLine();
temp=new IntegerCoffee.intValue();//temp=Integer.parseIntCoffee;
a = new int[temp];
System.out.println("Input "+ temp +" numbers. One per line!");
for ( i=0; i<a.length; i++)
{ c=keyin.readLine();int d=new IntegerCoffee.intValue();
a[i]=d; }
System.out.println("After sorting!");
for ( i=0; i<a.length-1; i++)
{k = i;
for( j = i + 1 ; j< a.length; j++)
   if (a[j]< a[k] )
     k = j;
     temp = a[i]; a[i]= a[k]; a[k] = temp;
  }
for ( i=0; i<a.length; i++)
System.out.println("第"+i+"个数组元素a"+i+"是:"+a[i]);
}
}

运行结果显示异常。本意是要进行数组元素值的输入,并完成排序;但总在输入输入第一个元素后、按下回车并接着输入第二个元素时出错:NumberFormatException。请各位高手指点,不胜感激。

2.Re:请教各位高手关于readline方法的NumberFormatException问题 [Re: tyrochen] Copy to clipboard
Posted by: tyrochen
Posted on: 2008-03-18 08:34

我用的IDE是JCreatorV4LE,在这个IDE下运行出现上述错误。但是,在DOS下运行却没有任何问题。
问题在各位帮助下已经解决。谢过各位了!

3.Re:请教各位高手关于readline方法的NumberFormatException问题 [Re: tyrochen] Copy to clipboard
Posted by: sunjinlong
Posted on: 2008-03-20 09:11

这是我修改后的。你看一下
import java.io.*;
class ArraySort
{
public static void main(String[] args) throws IOException
{
BufferedReader keyin =new BufferedReader(new InputStreamReader(System.in));
int j, i,k, temp;
String c;
System.out.println("Input the number of array elements!");
c=keyin.readLine();
temp=Integer.parseIntCoffee;
int a[]=new int[10];
System.out.println("Input "+ temp +" numbers. input 0 will stop:");
for ( i=0;; i++)
{
c=keyin.readLine();
int d=Integer.parseIntCoffee;
if(d==0) break;
a[i]=d;
}
System.out.println("After sorting!");
for ( i=0; i<temp-1; i++)
{
k=i;
for(j=i+1;j<temp;j++)
if (a[j]<a[k])
k=j;
int temp2=a[i];
a[i]=a[k];
a[k]=temp2;
}
for ( i=0; i<temp; i++)
System.out.println("第"+(i+1)+"个数组元素a"+i+"是:"+a[i]);
}
}

4.Re:请教各位高手关于readline方法的NumberFormatException问题 [Re: tyrochen] Copy to clipboard
Posted by: sunjinlong
Posted on: 2008-03-20 09:21

NumberFormatException:字符串到数字格式非法转换


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923