Problem Statement

Write a program that calculates the minimum, maximum, sum, and average of a list of numbers. The numbers should be read from a file provided as a command-line argument. The file should contain a single number on each line.

The program should handle all reasonable exceptions. If the input file is not provided or cannot be read, it should print "Error opening file." If the input file contains a value that cannot be converted to a number, it should print "Error parsing input." Any other errors should cause the program to print "Unknown error."

When printing, all decimal values should be rounded to the nearest tenth (one decimal place). The final output of the program should look similar to the following:

Minimum: 24.0
Maximum: 183.0
Sum: 1236.4
Average: 102.3

Uhhhhh...

Image Credit: Giphy

Let's break it down into smaller parts




Use Functions!

Image Credit: Giphy

Let's break it down into functions


Read Input File

import java.util.Scanner;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;

public class Example{
static double[] readInput(String filename){ try( Scanner scanner = new Scanner(Paths.get(filename)) ){
// Read input file here
}catch(InvalidPathException e){ System.out.println("Error opening file."); return new double[0]; }catch(NoSuchFileException e){ System.out.println("Error opening file."); return new double[0]; }catch(NumberFormatException e){ System.out.println("Error parsing input."); return new double[0]; }catch(IOException e){ System.out.println("Unknown error."); return new double[0]; } } }


Read Input File

import java.util.Scanner;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;

public class Example{
static double[] readInput(String filename){ try( Scanner scanner = new Scanner(Paths.get(filename)) ){
// Read input file here
}catch(InvalidPathException e){ System.out.println("Error opening file."); return new double[0]; }catch(NoSuchFileException e){ System.out.println("Error opening file."); return new double[0]; }catch(NumberFormatException e){ System.out.println("Error parsing input."); return new double[0]; }catch(IOException e){ System.out.println("Unknown error."); return new double[0]; } } }

Read Input File

import java.util.Scanner;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;

public class Example{
static double[] readInput(String filename){ try( Scanner scanner = new Scanner(Paths.get(filename)); Scanner scanner2 = new Scanner(Paths.get(filename)) ){
// Read input file here
}catch(InvalidPathException e){ System.out.println("Error opening file."); return new double[0]; }catch(NoSuchFileException e){ System.out.println("Error opening file."); return new double[0]; }catch(NumberFormatException e){ System.out.println("Error parsing input."); return new double[0]; }catch(IOException e){ System.out.println("Unknown error."); return new double[0]; } } }



Read Input File

import java.util.Scanner;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;

public class Example{
static double[] readInput(String filename){ try( Scanner scanner = new Scanner(Paths.get(filename)); Scanner scanner2 = new Scanner(Paths.get(filename)) ){
// Read input file here
}catch(InvalidPathException e){ System.out.println("Error opening file."); return new double[0]; }catch(NoSuchFileException e){ System.out.println("Error opening file."); return new double[0]; }catch(NumberFormatException e){ System.out.println("Error parsing input."); return new double[0]; }catch(IOException e){ System.out.println("Unknown error."); return new double[0]; } } }


Read Input File

// read the input
double[] numbers = new double[???];



Read Input File

// read the number of lines
int lines = 0;
while(scanner.hasNext()){
  scanner.nextLine(); // read the next line, but don't store it
  lines++;
}

// read the input double[] numbers = new double[lines];


Read Input File

// read the number of lines
int lines = 0;
while(scanner.hasNext()){
  scanner.nextLine(); // read the next line, but don't store it
  lines++;
}

// read the input double[] numbers = new double[lines];
int index = 0; while(scanner2.hasNext()){ String line = scanner2.nextLine(); numbers[index] = Double.parseDouble(line); index++; }

Read Input File

// read the number of lines
int lines = 0;
while(scanner.hasNext()){
  scanner.nextLine(); // read the next line, but don't store it
  lines++;
}

// read the input double[] numbers = new double[lines];
int index = 0; while(scanner2.hasNext()){ String line = scanner2.nextLine(); numbers[index] = Double.parseDouble(line); index++; }
// return the input return numbers;


Max & Min

static double min(double[] numbers){
  double minimum = 0;
  if(numbers.length > 0){
    minimum = numbers[0];
  }
  for(double d : numbers){
    if(d < minimum){
      minimum = d;
    }
  }
  return minimum;
}

static double max(double[] numbers){ double maximum = 0; if(numbers.length > 0){ maximum = numbers[0]; } for(double d : numbers){ if(d > maximum){ maximum = d; } } return maximum; }


Sum & Average

static double sum(double[] numbers){
  double sum = 0;
  for(double d : numbers){
    sum += d;
  }
  return sum;
}

static double avg(double[] numbers){ double sum = 0; for(double d : numbers){ sum += d; } return sum / numbers.length; }

Sum & Average

static double sum(double[] numbers){
  double sum = 0;
  for(double d : numbers){
    sum += d;
  }
  return sum;
}

static double avg(double[] numbers){ double sum = 0; for(double d : numbers){ sum += d; } return sum / numbers.length; }


Sum & Average

static double sum(double[] numbers){
  double sum = 0;
  for(double d : numbers){
    sum += d;
  }
  return sum;
}

static double avg(double[] numbers){ double sum = sum(numbers); return sum / numbers.length; }


Output

static void printOutput(double minimum, double maximum, double sum, double avg){
  System.out.println(String.format("Minimum: %.1f", minimum));
  System.out.println(String.format("Maximum: %.1f", maximum));
  System.out.println(String.format("Sum: %.1f", sum));
  System.out.println(String.format("Average: %.1f", avg));
}


Main

import ...

public class Example{
public static void main(String[] args){
}
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}


Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}


Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}


Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); if(numbers.length == 0){ return; } }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}



Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); if(numbers.length == 0){ return; } double minimum = min(numbers); double maximum = max(numbers); double sum = sum(numbers); double average = avg(numbers); }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}



Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); if(numbers.length == 0){ return; } double minimum = min(numbers); double maximum = max(numbers); double sum = sum(numbers); double average = avg(numbers); printOutput(minimum, maximum, sum, average); }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}



Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); if(numbers.length == 0){ return; } double minimum = min(numbers); double maximum = max(numbers); double sum = sum(numbers); double average = avg(numbers); printOutput(minimum, maximum, sum, average); return; }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}



Main

import ...

public class Example{
public static void main(String[] args){ if(args.length != 1){ System.out.println("Error opening file."); return; } double[] numbers = readInput(args[0]); if(numbers.length == 0){ return; } double minimum = min(numbers); double maximum = max(numbers); double sum = sum(numbers); double average = avg(numbers); printOutput(minimum, maximum, sum, average); return; }
static double[] readInput(String filename){...} static double min(double[] numbers){...} static double max(double[] numbers){...} static double sum(double[] numbers){...} static double avg(double[] numbers){...} static void printOutput(double minimum, double maximum, double sum, double avg){...}
}