Q:

Java loops that use imported files

My code below works when a file imported contains 100 and more numbers. How do I get the loops to stop if there is less than 100 numbers left in the file? When there is less than 100 numbers, an error comes up as the array is not full.
try {
    Scanner in = new Scanner(new File(filename));
    int[] aNums = new int[100];
    System.out.print("Array contents: ");
    int i =0;
    while (i < aNums.length){
        generated = in.nextInt() ;
        aNums[i] = generated;
        System.out.print( aNums[i] );
        System.out.print(" ");
        i++;
    }
    System.out.println(" ");
    System.out.println(" ");
    System.out.print("Array contents: ");
    for (i = aNums.length - 1; i >= 0; i--) {
        System.out.print( aNums[i] );
        System.out.print(" ");
    }
}
// Catch block, needed when using file input
catch (FileNotFoundException e) {
    System.out.println("That file was not found. Program terminating...");
    e.printStackTrace();
}

A:

Just check the scanner if the file has more values:
while (i < len && in.hasNextInt())

