Once a Scanner is created for a file of integers, the Scanner hasNextInt() method can be used as the while loop test for end of input.
The initialization of the max should be the first value read (if the file is not empty). This changes the code a bit from the sentinel example:
1 public class SimpleFileInput { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 String fileName; 8 Scanner input = new Scanner(System.in); 9 Scanner infile = null; 10 int max = 0, n; 11 12 System.out.println("This program will read integers from a file and print the largest value."); 13 System.out.printf("\nEnter the input file name: "); ... /* code to create a Scanner for infile goes here, but is not shown */ ... 24 int count = 0; 25 while(infile.hasNextInt()) { 26 if (count == 0) { 27 max = infile.nextInt(); 28 } else { 29 n = infile.nextInt(); 30 if ( n > max ) { 31 max = n; 32 } 33 } 34 count++; 35 } 36 System.out.printf("There were %d values read from input file %s.\n", count, fileName); 37 if ( count > 0 ) { 38 System.out.printf("The maximum value read was %d\n", max); 39 } 40 41 } 42 }