import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; public class Main { public static void processFile(String fname) throws FileNotFoundException, InputMismatchException, NegativeIntegerException { Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); if (x < 0) throw new NegativeIntegerException(); System.out.println("Data is: " + x); inFile.close(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean shouldEnd = false; while (!shouldEnd) { try { System.out.print("Input the file pathname: "); String fname = in.next(); processFile(fname); shouldEnd = true; } catch (FileNotFoundException e) { System.out.println("Cannot open the file."); System.out.print("Try another file? Type your choice [y/n]: "); shouldEnd = (in.next().charAt(0) == 'n'); } catch (InputMismatchException e) { System.out.println("Cannot read the required number from the opened file."); System.out.print("Try another file? Type your choice [y/n]: "); shouldEnd = (in.next().charAt(0) == 'n'); } catch (NegativeIntegerException e) { System.out.println("Unexpected negative value from the file."); System.out.print("Try another file? Type your choice [y/n]: "); shouldEnd = (in.next().charAt(0) == 'n'); } } in.close(); } } /* * Rundown 1 */ /* * Rundown 2 */