java程序的问题,请教高手

franz lee 2007-09-03 04:04:56
//未编译成功,有错
package file;
import java.io.*;
public class filelist{
public static void main(String [] args)throws IOException
{
int sum=0,current=0;
//设置输入流
BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
//决定文件名
System.out.println("please input filename:");
String filename=std.readLine();
//设置文件流
BufferedReader fileIn=new BufferedReader(new FileReader(filename));
//按行进行处理
String currentline=fileIn.readLine();
if(currentline==null)
{
System.out.println("文件是不是空的哟");
}
else
{ System.out.println("文件不是空的,嘿嘿");
System.out.println("文件中的数据是:");
do
{
// System.out.println(currentline);
int read=Integer.parseInt(currentline);
System.out.print(read+" ");
sum+=read;
// current++;
++current;
currentline=fileIn.readLine();
}
while(currentline!=null);
fileIn.close(); //关闭文件输入流
System.out.println();
System.out.println("它们的和为:");
System.out.println(sum);
System.out.println("它们的个数为:");
System.out.println(current);
float avg=sum/current;
System.out.println("它们的平均数为:");
System.out.println(avg);
}
}
}
结果是:
please input filename:
li1.txt
文件不是空的,嘿嘿
文件中的数据是:
1 2 3 4 5 6 7 8 9 java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:489)
at java.lang.Integer.parseInt(Integer.java:518)
at file.filelist.main(filelist.java:27)
Exception in thread "main"
Process completed.
我给的文件是:
name:lif1.txt
1
2
3
4
5
6
...全文
255 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
renke 2007-09-05
  • 打赏
  • 举报
回复
楼上说的对
wuhaozhiyuan 2007-09-05
  • 打赏
  • 举报
回复
int read = Integer.parseInt("");
它会抛出和你文件抛出的相同的异常.
应该是在你文件的最后出现了空行,也就是只有回车,没有内容.
king_freedom 2007-09-04
  • 打赏
  • 举报
回复
cmd javac java直接运行
liming1983 2007-09-04
  • 打赏
  • 举报
回复
For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:489)
at java.lang.Integer.parseInt(Integer.java:518)
意思就是说程序在将一个字符串转换为数字的时候发生了错误,而这个字符串就是""
franz lee 2007-09-04
  • 打赏
  • 举报
回复
For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:489)
at java.lang.Integer.parseInt(Integer.java:518)
这是什么意思?
liming1983 2007-09-04
  • 打赏
  • 举报
回复
请检察下那一列数字最后是否多了个回车换行?
franz lee 2007-09-03
  • 打赏
  • 举报
回复
我运行结果就是我我打出来的那个啊
我用的jcreator,你用的什么
king_freedom 2007-09-03
  • 打赏
  • 举报
回复
刚执行过了.没错.>~`直接复制下来运行的.~

62,623

社区成员

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

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