I have an input file with integers, I am obviously doing something wrong here, When I try to print the elements from the array, the integers are printing as 0's, and not even for the same amount. In the file there are 20 integers.

mport java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class insertMain {

    public static void main(String[] args)throws IOException  {

             Scanner inputFile = new Scanner(new File("input2.txt"));  
              int[] integers = new int [20];
             int count = 0;

            while(inputFile.hasNextInt())
            {
                for(int i = 0; i < integers.length; i++) {
                    int[] nums = new int[inputFile.nextInt()];
                    count++;
                   int num = inputFile.nextInt();
                  System.out.println(nums[i]);                
             }                
            }
            System.out.println("Count: " + count);
            inputFile.close(); 

    }
    }

This is what I have for output

 run:
0
0
0
0
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at insertMain.main(insertMain.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Can you please help guide me in the right direction... plus I don't think I need the original array of int[] integers

int[] nums = new int[inputFile.nextInt()];

this doesn't fill the element of the array, it creates a new array.

what you want is:

integers[i] = inputFile.nextInt();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.