How do I approach this program?
I need to fill in the code for this program. It is supposed to read
integer scores from a file and print out their average. We are supposed to
account for files that do not contain any integer values. The output is
just a simple print line displaying the averaged out scores.
he gives us a hint:"if input is a Scanner object associated with a file,
input.hasNextInt() returns true if there is an integer value left unread
in the file; otherwise it returns false."
import java.io.*;
import java.util.Scanner;
public class GradeAvg
{
public static void main(String[] args)
{
Scanner input = null;
double average=0.0;
try
{
input = new Scanner(new File("scores.dat"));
}
catch (FileNotFoundException e)
{
System.out.println("*** Can't open scores.dat ***");
System.exit(1);
}
System.out.println("The average of the test scores in the file are: ");
}
}
No comments:
Post a Comment