Question
// Chapter 7 Question 13 // Finish this program, which reads integer scores // from a file and prints out their average. Hint: // if
// Chapter 7 Question 13
// Finish this program, which reads integer scores
// from a file and prints out their average. 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; input.nextInt() returns the
// integer read from the file.
//Make a simple while loop and print average
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AverageScore
{
public static void main(String[] args)
{
int total = 0;
int amountScore = 0;
Scanner input = null;
Scanner filenameterminal = new Scanner(System.in);
String file1 = "";
try
{
System.out.println("Enter the file name with the scores: ");
file1 = filenameterminal.nextLine(); // at terminal, enter scores.dat
input = new Scanner(new File(file1));
}
catch (FileNotFoundException e)
{
System.out.println("*** Can't open " + file1 + " ***");
System.exit(1);
}
while(input.nextInt()) {
total += input;
amountScore++;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started