Problem Statement

Write a program to accept a list of numbers, one per line. Input is provided from a file if one is provided as the first command line argument. If a file is not provided, input should be read from the terminal.

The numbers can either be whole numbers or floating point numbers. The program should continue to accept input until a number equal to 0 is input. Of course, if the input in a file ends before a 0, that can be treated as the end of the input.

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input). Sample outputs are shown below.

The program should catch and handle all common exceptions. When an exception is caught, the program should print the name of the exception, followed by the message included in the exception. It should then continue to accept input (if possible). Specifically, if it is unable to open the file provided, input should be read from the terminal.

Zoinks!

Image Credit: Boomerang via Giphy

Let's break it down into smaller parts

public class Example{

public static void main(String[] args){

} }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;

public class Example{

public static void main(String[] args){ Scanner scanner; scanner = new Scanner(new File(args[0])); } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;

public class Example{

public static void main(String[] args){ Scanner scanner; scanner = new Scanner(new File(args[0])); } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;

public class Example{

public static void main(String[] args) throws… { Scanner scanner; scanner = new Scanner(new File(args[0])); } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;

public class Example{

public static void main(String[] args) throws… { Scanner scanner; scanner = new Scanner(new File(args[0])); } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); } } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); scanner = new Scanner(System.in); } } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); scanner = new Scanner(System.in); } } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;
import Java.lang.ArrayIndexOutOfBoundsException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); scanner = new Scanner(System.in); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(“ArrayIndexOutOfBounds…"); scanner = new Scanner(System.in); } } }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;
import Java.lang.ArrayIndexOutOfBoundsException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); scanner = new Scanner(System.in); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(“ArrayIndexOutOfBounds…"); scanner = new Scanner(System.in); }

<mark>try(
  Scanner reader = scanner
){</mark>

    // more code here

<mark>}catch(Exception e){
  System.out.println("Exception: " + e);
}</mark>

} }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

import java.util.Scanner;
import java.io.File;
import Java.io.FileNotFoundException;
import Java.lang.ArrayIndexOutOfBoundsException;

public class Example{

public static void main(String[] args){ Scanner scanner; try{ scanner = new Scanner(new File(args[0])); }catch(FileNotFoundException e){ System.out.println(“FileNotFoundException: " …); scanner = new Scanner(System.in); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(“ArrayIndexOutOfBounds…"); scanner = new Scanner(System.in); }

try(
  Scanner reader = scanner
){

    <mark>// more code here</mark>

}catch(Exception e){
  System.out.println("Exception: " + e);
}

} }

Input is provided from a file [or] read from the terminal.

The program should catch and handle all common exceptions.

while(true){
  double input = reader.nextDouble();
  if(input == 0.0){
    break;
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  double input = reader.nextDouble();
  if(input == 0.0){
    break;
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  double input = reader.nextDouble();
  if(input == 0.0){
    break;
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  double input = reader.nextDouble();
  if(input == 0.0){
    break;
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  try{
    double input = reader.nextDouble();
    if(input == 0.0){
      break;
    }
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  try{
    double input = reader.nextDouble();
    if(input == 0.0){
      break;
    }
  }catch(InputMismatchException e){
    System.out.println("InputMismatchException");
    reader.next(); // ignore bad input
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  try{
    double input = reader.nextDouble();
    if(input == 0.0){
      break;
    }
  }catch(InputMismatchException e){
    System.out.println("InputMismatchException");
    reader.next(); // ignore bad input
  }catch(NoSuchElementException e){
    System.out.println("NoSuchElementException");
    break; // out of input, so we should stop there
  }
}

list of numbers, one per line... whole numbers or floating point numbers... accept input until a number equal to 0 is input.

The program should catch and handle all common exceptions.

while(true){
  try{
    double input = reader.nextDouble();
    if(input == 0.0){
      break;
    }
  }catch(InputMismatchException e){
    System.out.println("InputMismatchException");
    reader.next(); // ignore bad input
  }catch(NoSuchElementException e){
    System.out.println("NoSuchElementException");
    break; // out of input, so we should stop there
  }
}

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input).

double minimum = 0.0;
double maximum = 0.0;

while(true){ try{ double input = reader.nextDouble(); if(input == 0.0){ break; } }catch(InputMismatchException e){ System.out.println(“InputMismatchException”); reader.next(); // ignore bad input }catch(NoSuchElementException e){ System.out.println(“NoSuchElementException”); break; // out of input, so we should stop there } }

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input).

double minimum = 0.0;
double maximum = 0.0;

while(true){ try{ double input = reader.nextDouble(); if(input == 0.0){ break; } if(input < minimum || minimum == 0.0){ minimum = input; } if(input > maximum || maximum == 0.0){ maximum = input; } }catch(InputMismatchException e){ System.out.println(“InputMismatchException”); reader.next(); // ignore bad input }catch(NoSuchElementException e){ System.out.println(“NoSuchElementException”); break; // out of input, so we should stop there } }

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input).

double minimum = 0.0;
double maximum = 0.0;

while(true){ try{ double input = reader.nextDouble(); if(input == 0.0){ break; } if(input < minimum || minimum == 0.0){ minimum = input; } if(input > maximum || maximum == 0.0){ maximum = input; } }catch(InputMismatchException e){ System.out.println(“InputMismatchException”); reader.next(); // ignore bad input }catch(NoSuchElementException e){ System.out.println(“NoSuchElementException”); break; // out of input, so we should stop there } }

System.out.println(“Maximum: " + maximum); System.out.println(“Minimum: " + minimum);

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input).

double minimum = 0.0;
double maximum = 0.0;

while(true){ try{ double input = reader.nextDouble(); if(input == 0.0){ break; } if(input < minimum || minimum == 0.0){ minimum = input; } if(input > maximum || maximum == 0.0){ maximum = input; } }catch(InputMismatchException e){ System.out.println(“InputMismatchException”); reader.next(); // ignore bad input }catch(NoSuchElementException e){ System.out.println(“NoSuchElementException”); break; // out of input, so we should stop there } }

System.out.println(“Maximum: " + maximum); System.out.println(“Minimum: " + minimum);

Once 0 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 0 to end input).

"/js/highlight.pack.js"