该程序不读取该文件

时间:2014-04-01 12:45:10

标签: java ioexception

任何人都可以告诉我为什么这个程序不起作用。你应该创建一个汇总双值的文件。当我运行程序时,它会读取文件但不显示总和。

import java.util.*;
import java.io.*;
public class TheGradesArrays
{
public static void main(String [] args)
{
    Scanner scan = new Scanner (System.in);
    Scanner infile=null;
    boolean isValid;
    String name;
    do
    {
        isValid=true;
        System.out.print("Enter the name of the file: ");
        name=scan.nextLine();
        try
        {       
            infile = new Scanner (new FileInputStream (name+".txt"));
        }
        catch(IOException fe)
        {
            System.out.println("The name is not valid! Renter the name."+fe);
            fe.printStackTrace();
            isValid=false;

        }
    }while(! isValid );

    double sum=0,grade;
    while(infile.hasNextDouble());
    {
        grade=infile.nextDouble();
        sum+=grade;
    }

    System.out.print("The Sum = "+sum);

}
}

1 个答案:

答案 0 :(得分:5)

更改无限循环

while(infile.hasNextDouble());

进入正确的循环标题:

while(infile.hasNextDouble())