这个java代码哪里不对啊 Exception in thread "main" java.lang.NumberFormatException: null at java.la
importjava.io.BufferedReader;importjava.io.FileNotFoundException;importjava.io.FileRe...
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
//
public class IOLianXi
{
public static void main(String qrgs [] ) {
try{ BufferedReader inputStream = new BufferedReader(new FileReader("data.txt"));
String temp = null;
double sum = 0;
int count = 0;
int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
while((temp = inputStream.readLine())!=null);{
int num = Integer.parseInt(temp);
if(num > max) {
max = num;
}
if(num < min) {
min = num;
sum += num;
count++;
}
}
System.out.println("max ="+max);
System.out.println("min ="+min);
System.out.println("Average ="+sum/count);
PrintWriter outputStream = new PrintWriter(new FileWriter("result.txt"));
outputStream.println(max);
outputStream.println(min);
outputStream.println(sum/count);
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
最后报错
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at ch01.IOLianXi.main(IOLianXi.java:21) 展开
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
//
public class IOLianXi
{
public static void main(String qrgs [] ) {
try{ BufferedReader inputStream = new BufferedReader(new FileReader("data.txt"));
String temp = null;
double sum = 0;
int count = 0;
int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
while((temp = inputStream.readLine())!=null);{
int num = Integer.parseInt(temp);
if(num > max) {
max = num;
}
if(num < min) {
min = num;
sum += num;
count++;
}
}
System.out.println("max ="+max);
System.out.println("min ="+min);
System.out.println("Average ="+sum/count);
PrintWriter outputStream = new PrintWriter(new FileWriter("result.txt"));
outputStream.println(max);
outputStream.println(min);
outputStream.println(sum/count);
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
最后报错
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at ch01.IOLianXi.main(IOLianXi.java:21) 展开
1个回答
2013-04-21 · 知道合伙人数码行家
关注
展开全部
while((temp = inputStream.readLine())!=null);{
改为:
while((temp = inputStream.readLine())!=null){
多了一个“;”号啊
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
//
public class IOLianXi {
public static void main(String qrgs[]) {
try {
BufferedReader inputStream = new BufferedReader(new FileReader(
"data.txt"));
String temp = null;
double sum = 0;
int count = 0;
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
while ((temp = inputStream.readLine()) != null)
{
int num = Integer.parseInt(temp);
if (num > max) {
max = num;
}
if (num < min) {
min = num;
sum += num;
count++;
}
}
System.out.println("max =" + max);
System.out.println("min =" + min);
System.out.println("Average =" + sum / count);
PrintWriter outputStream = new PrintWriter(new FileWriter(
"result.txt"));
outputStream.println(max);
outputStream.println(min);
outputStream.println(sum / count);
outputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询