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 { Scanner inFile = new Scanner(new File(fname)); int x = inFile.nextInt(); 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'); } } in.close(); } } /* Rundown 1 Input the file pathname: i:\data002.txt Cannot read the required number from the opened file. Try another file? Type your choice [y/n]: y Input the file pathname: i:\data01.txt Cannot open the file. Try another file? Type your choice [y/n]: y Input the file pathname: i:\data001.txt Data is: 678 */ /* Rundown 2 Input the file pathname: i:\data02.txt Cannot open the file. Try another file? Type your choice [y/n]: y Input the file pathname: i:\data01.txt Cannot open the file. Try another file? Type your choice [y/n]: n */