Read Array From User in Java
Problem
Console input lets a program read an array's size and elements typed by the user while it's running, instead of relying only on hard-coded values.
Read the number of elements and the array elements from the console, then print the array.
Java Program
import java.util.Arrays;
import java.util.Scanner;
public class ReadArrayFromUser {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
System.out.println(n);
System.out.println("Enter " + n + " elements:");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt(); // read one element per iteration
}
System.out.println("Array: " + Arrays.toString(arr));
}
}Output
Core Logic
Reading the size first, then looping that many times to read each element, builds the array one value at a time from console input.
- 1
new Scanner(System.in)creates a reader attached to the console. - 2
sc.nextInt()reads the array's sizen, which is then echoed back withprintln. - 3
int[] arr = new int[n]creates an array sized exactly to holdnelements. - 4A loop from
0ton - 1callssc.nextInt()once per iteration, filling each slot ofarrin turn.
5 for the size, then 5 12 8 21 3 for the elements, fills the array and prints Array: [5, 12, 8, 21, 3].Key Point: nextInt() reads one whitespace-separated token at a time, so the five elements can be typed on one line or across several — the loop doesn't care which.
Why: The array is filled with n values read from input, so both the reading time and the storage grow with n.
Key Concepts
Approach 2: BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class ReadArrayFromUserBufferedReader {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements: ");
int n = Integer.parseInt(br.readLine());
System.out.println(n);
System.out.println("Enter " + n + " elements:");
// readLine() returns the whole line, so split() breaks it into individual tokens
String[] tokens = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(tokens[i]);
}
System.out.println("Array: " + Arrays.toString(arr));
}
}
Output
Core Logic
BufferedReader is a common alternative to Scanner — read the elements as one line of text, then split and parse it yourself.
- 1
new BufferedReader(new InputStreamReader(System.in))wraps standard input in a buffered character reader. - 2
br.readLine()reads the size as a line of text, parsed into anintwithInteger.parseInt(). - 3A second
br.readLine()reads the whole line of elements at once, then.split(" ")breaks it into individual tokens. - 4Each token is parsed with
Integer.parseInt()and stored intoarr.
"5 12 8 21 3" and splitting it on spaces produces five tokens, parsed into the same array as the Scanner version.Key Point: BufferedReader is noticeably faster than Scanner for reading large volumes of input, which is why it's the preferred choice in competitive programming — the trade-off is the extra manual splitting and parsing step.
Why: split() allocates an array of string tokens before each one is parsed into the int array, so both scale with the number of elements.