Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Influxdb Database A Comprehensive Guide To Learn Influxdb Database

Authors: Cybellium Ltd ,Kris Hermans

1st Edition

B0CNGGWL7B, 979-8867766450

More Books

Students also viewed these Databases questions

Question

(f) What type of factorial design it is? Pg45

Answered: 1 week ago