Using a scanner is much faster and therefore more efficient. Furthermore, this does not require you to encounter the problem of using buffered streams for input. Here is its use:
java.util.Scanner sc = new java.util.Scanner(System.in); // "System.in" is a stream, a String or File object could also be passed as a parameter, to take input from int n; // take n as input or initialize it statically int ar[] = new int[n]; for(int a=0;a<ar.length;a++) ar[a] = sc.nextInt(); // ar[] now contains an array of n integers
Also note that the nextInt() function may throw 3 exceptions, as indicated here . Do not forget to handle them.
source share