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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!