import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.NoSuchElementException; public class Ex { public static void main(String [] args) { Scanner inFile = null; Scanner kbd = new Scanner(System.in); int numStrikes = 0; while (true) { System.out.printf("Please enter file name: "); String fileName = kbd.next(); try { inFile = new Scanner(new FileInputStream(fileName)); } catch(FileNotFoundException e) { System.out.printf("Sorry, I can't open that file. Try again.\n"); ++numStrikes; if (numStrikes == 3) { System.out.printf("You obviously are trying to waste my time.\n GO AWAY!\n"); return; } continue; } // If we get to this point, then we have the file. break; } // Now we can read the file! try { int firstNumber = inFile.nextInt(); System.out.printf("The first number in the file is %d\n", firstNumber); } catch(NoSuchElementException e) { System.out.printf("There is no number atop the file. :( \n"); System.out.printf("Do think I was born yesterday?\n"); } System.out.printf("Program continues...\n"); } }