Q:

Storing numbers from a file into an array

I have a file called "input.txt" with the numbers below. I want to read this file and make it into an array. The first integer - 10 tells the number of indexes or elements in the array, so 10 spots. The code below is what I have done so far I just don't know how to read the input.txt from the first line
    10
    3
    4
    5
    6
    4
    4
    5
    6
    3

size of this array will be the first number in the file
static void display(int[] numArray) {
    System.out.println("Array contents: ");
    for (int i = 0; i < numArray.length; i++) {
        System.out.print(numArray[i] + " ");
    }
    System.out.println();
}

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int[] numberArray = new int[10];

    System.out.println("Enter the name of your file (including file extension): ");
    String filename = input.next();

    int count = 0;

    try {

        Scanner in = new Scanner(new File(filename));
        int result = in.nextInt();

        while (in.hasNextInt()&& count < 10) {
            numberArray[count] = result;
            count = count + 1;
            result = in.nextInt();
        }
    } catch (FileNotFoundException e) {
        System.out.println("That file was not found. Program terminating...");
        e.printStackTrace();
    }

    display(numberArray);

}

A:

You need create the array with size of first line and, if you need, put in the array. You can try these ways:
If you are using Java 7 or above, you can try something like this:
public static void main(String[] args) {
    try (final Scanner input = new Scanner(System.in)) {
        final int[] numberArray = new int[10];

        System.out.println("Enter the name of your file (including file extension): ");
        final String filename = input.next();

        try (final Scanner in = new Scanner(new File(filename))) {
            final int count = in.nextInt();
            for (int i = 0; in.hasNextInt() && i < count; i++) {
                numberArray[i] = in.nextInt();
            }
            display(numberArray);
        } catch (final FileNotFoundException e) {
            System.out.println("That file was not found. Program terminating...");
            e.printStackTrace();
        }
    }
}

If you are using java before 7, try this:
public static void main(String[] args) {
    try (final Scanner input = new Scanner(System.in)) {

        System.out.println("Enter the name of your file (including file extension): ");
        final String filename = input.next();

        int[] numberArray = null;
        try (final Scanner in = new Scanner(new File(filename))) {
            final int count = in.nextInt();

            numberArray = new int[count];

            for (int i = 0; in.hasNextInt() && i < count; i++) {
                numberArray[i] = in.nextInt();
            }
            display(numberArray);
        } catch (final FileNotFoundException e) {
            System.out.println("That file was not found. Program terminating...");
            e.printStackTrace();
        }
    }
}

If the first line should be added to the array, put numberArray[0] = count;
 below numberArray = new int[count]; and initialize for step with 1.

