Java ProgramsArraysRead Array From User

Read Array From User in Java

beginner·  Arrays  ·  I/O

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.

Input
5, then 5 12 8 21 3
Output
Enter number of elements: 5 Enter 5 elements: Array: [5, 12, 8, 21, 3]

Java Program

Java
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

Enter number of elements: 5 Enter 5 elements: Array: [5, 12, 8, 21, 3]

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.

How It Works
  1. 1new Scanner(System.in) creates a reader attached to the console.
  2. 2sc.nextInt() reads the array's size n, which is then echoed back with println.
  3. 3int[] arr = new int[n] creates an array sized exactly to hold n elements.
  4. 4A loop from 0 to n - 1 calls sc.nextInt() once per iteration, filling each slot of arr in turn.
Entering 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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The array is filled with n values read from input, so both the reading time and the storage grow with n.

Key Concepts

ScannernextInt()array initialization in a loop

Approach 2: BufferedReader

Java
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

Enter number of elements: 5 Enter 5 elements: Array: [5, 12, 8, 21, 3]

Core Logic

BufferedReader is a common alternative to Scanner — read the elements as one line of text, then split and parse it yourself.

How It Works
  1. 1new BufferedReader(new InputStreamReader(System.in)) wraps standard input in a buffered character reader.
  2. 2br.readLine() reads the size as a line of text, parsed into an int with Integer.parseInt().
  3. 3A second br.readLine() reads the whole line of elements at once, then .split(" ") breaks it into individual tokens.
  4. 4Each token is parsed with Integer.parseInt() and stored into arr.
Reading the line "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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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.

Key Concepts

BufferedReaderreadLine()String.split()

Related Programs