=================== Step 0: start up =================== import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); inFile.close(); System.out.println("Data is: " + x); System.out.print("Done."); in.close(); System.out.print("Program ended."); } } // may throw FileNotFoundException !! (abc.txt) // may throw InputMismatchException !! (data002.txt) NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, StackOverflowError =================== Step 1: processFile method =================== import java.util.*; import java.io.*; public class Main { public static void processFile(String fname) throws FileNotFoundException { Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); inFile.close(); System.out.println("Data is: " + x); } public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); processFile(fname); System.out.print("Done."); in.close(); System.out.print("Program ended."); } } // may throw FileNotFoundException !! (abc.txt) // may throw InputMismatchException !! (data002.txt) // NullPointerException // ArrayIndexOutOfBoundsException // ClassCastException // StackOverflowError =================== Step 2: add exception handling =================== import java.util.*; import java.io.*; public class Main { public static void processFile(String fname) throws FileNotFoundException { Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); inFile.close(); System.out.println("Data is: " + x); } public static void main(String[] args) { Scanner in = new Scanner(System.in); try { System.out.print("Input the file pathname: "); String fname = in.next(); processFile(fname); System.out.print("Done."); } catch (FileNotFoundException e) { System.out.println("Cannot open the file."); } catch (InputMismatchException e) { System.out.println("Cannot read the required number from the opened file."); } in.close(); System.out.print("Program ended."); } } //throws FileNotFoundException, InputMismatchException =================== Step 3: add negative integer checking =================== import java.util.*; import java.io.*; public class Main { public static void processFile(String fname) throws FileNotFoundException, NegativeIntegerException { Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); inFile.close(); if (x < 0) throw new NegativeIntegerException(); System.out.println("Data is: " + x); } public static void main(String[] args) { Scanner in = new Scanner(System.in); try { System.out.print("Input the file pathname: "); String fname = in.next(); processFile(fname); } catch (FileNotFoundException e) { System.out.println("Cannot open the file."); } catch (InputMismatchException e) { System.out.println("Cannot read the required number from the opened file."); } catch (NegativeIntegerException e) { System.out.println("Unexpected negative value from the file."); } System.out.print("Done."); in.close(); } } ======================= class NegativeIntegerException extends Exception { public NegativeIntegerException() { super("Negative integer!"); } public NegativeIntegerException(String message) { super(message); } }