/**
 * Read series of integers from file,
 * output result of dividing 10 by each of the numbers
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class GiantExample {

	public static void main(String[] args) {
		// Construct scanner to read keyboard input
		Scanner in = new Scanner(System.in);
		System.out.print("Enter a filename: ");
		String filename = in.next();
		
		try {
			readFile(filename);
		} catch (/*FileNotFound*/Exception e) {
			System.out.println("main: encountered " + e);
		}
		
		System.out.println("All done!");
		
		in.close();
	}
	
	// Open and read the file specified by filename, and "process" each int
	private static void readFile(String filename) 
			throws FileNotFoundException, ArithmeticException {
		
		Scanner input; 
		/*
		// Version 1: try/catch each exception separately
		try {
			// Open a new File and construct a Scanner to read from the File
			input = new Scanner(new File(filename));
		} catch (FileNotFoundException e) {
			System.out.println("readFile: encountered " + e);
			throw e;
		}
		
		// Continue reading the file as long as there's another int
		while (input.hasNextInt()) {
			int n = input.nextInt();
			System.out.println("Read int " + n);
			try { 
				int result = tenDivideByN(n);
				System.out.println("10/" + n + " = " + result);
			} catch (ArithmeticException e) {
				System.out.println("readFile: encountered " + e);
				throw e;
			}
		}
		*/
		try {
			// Open a new File and construct a Scanner to read from the File
			input = new Scanner(new File(filename));
			// Continue reading the file as long as there's another int
			while (input.hasNextInt()) {
				int n = input.nextInt();
				System.out.println("Read int " + n);
				int result = tenDivideByN(n);
				System.out.println("10/" + n + " = " + result);
			}
		} catch (FileNotFoundException e) {
			System.out.println("readFile: encountered " + e);
			throw e;
		} catch (ArithmeticException e) {
			System.out.println("readFile: encountered " + e);
			throw e;
		}
		
		// Close the Scanner
		input.close();
	}
	
	private static int tenDivideByN(int n) throws ArithmeticException {
		try {
			int result = 10 / n;

			return result;
		} catch (ArithmeticException e) {
			System.out.println("tenDivideByN: encountered " + e);
			throw e;
		}
	}
	
}
