Suppose that a text file Lab2.txt contains an unspecified number of scores. Writ
ID: 3628851 • Letter: S
Question
Suppose that a text file Lab2.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average. In the text file, the scores are separated by blanks.The program should contain two methods with these signatures (it can contain other methods besides these if you think appropriate):
public static double totalScores(File inFile)
public static double averageScores(File inFile)
The output from the program should look like this successful sample run from my solution:
The total of the scores is 60.7
The average of the scores is 6.07
Once you have the program working, implement exception handling to handle FileNotFoundException.
Explanation / Answer
package javaapplication4;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JavaApplication4
{
//List variable for storing the read scores from the file
public static List<Double> Values;
public static void main(String[] args)
{
//Creating File for reading
File inFile=new File("C:\MyData\input\Lab2.txt");
//Initializing the List
Values = new ArrayList<Double>();
//Calling the method that calculates the Avearge
double avg= averageScores(inFile);
System.out.println("The average of the scores is " + avg);
}
public static void readFile(File inFile)
{
BufferedReader reader = null;
try
{
//Inorder to read a file, we need to use FileReader and then to store the results temporarily
//we need to use buffer reader
reader = new BufferedReader(new FileReader(inFile));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
//The read values will be in text format, convert them to double values
Values.add(Double.parseDouble(text));
}
}
catch (FileNotFoundException e)
{
//Handling FileNotFoundException
System.out.println(e.getMessage());
}
catch (IOException e)
{
//Handling other IO and File Readingt related Exception
System.out.println(e.getMessage());
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
public static double totalScores(File inFile)
{
double sum =0.0;
//Calling File Read methods
readFile(inFile);
//Calculating the total
for(int i=0;i<Values.size();i++)
{
sum += Values.get(i);
}
return sum;
}
public static double averageScores(File inFile)
{
//Calling the method which calculates the total of all scores
double total = totalScores(inFile);
System.out.println("The total of the scores is " + total);
int len=Values.size();
//Condition check for divide by zero error
if(len != 0)
{
return total/len;
}
return 0.0;
}
}
Note: Please make sure the path of the input file while running it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.