How to Take Array Input From User in Java?
Last Updated :
17 Apr, 2025
Arrays in Java are an important data structure, and we can add elements to them by taking input from the user. There is no direct method to take input from the user, but we can use the Scanner Class or the BufferedReader class, or the InputStreamReader Class.
1. Using Scanner Class to Take Array Input
Approach:
- First, we create an object of the Scanner Class and import the java.util.Scanner package.
- Then create a variable for taking the size of the array from the user, and show the message “Enter size of array“
- Then we use the hasNextInt() and nextInt() methods of the Scanner class to take integer input from the user through the console.
- Then we print each element of the array using a loop.
- Then we close the Scanner object when we have completed the program for best practice.
Example: Taking input from the user in a one-dimensional array using the Scanner class and loops.
Java
// Java program to take 1D array
// input from the user
// using Scanner class and loops
import java.util.Scanner;
public class Geeks
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Take the array size from the user
System.out.println("Enter the size of the array: ");
int s = 0;
if (sc.hasNextInt()) {
s = sc.nextInt();
}
// Initialize the array's
// size using user input
int[] arr = new int[s];
// Take user elements for the array
System.out.println(
"Enter the elements of the array: ");
for (int i = 0; i < s; i++) {
if (sc.hasNextInt()) {
arr[i] = sc.nextInt();
}
}
System.out.println(
"The elements of the array are: ");
for (int i = 0; i < s; i++) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}
Output:

Explanation: In this example, first we use the Scanner class to accept user input and save the array size in the size variable. Then we make an array arr with the size s. Following that, we use a for loop to accept user input and save it in an array. Finally, we use another for loop to print the array’s elements.
The Scanner class from java.util helps to take user input. We can use the Scanner class with loops to take input for individual array elements (to use this technique, we must know the length of the array).
In this example, we will understand how to take input for a two-dimensional array using the Scanner
class and loops.
Approach:
- First, we create an object of the Scanner Class and import the java.util.Scanner package.
- Then create two variables for taking the row and column size of the array from the user, show the message to enter the row and column one by one.
- Then we use the hasNextInt() and nextInt() methods of the Scanner class to take integer input from the user through the console with nested loop.
- Then we print each element of the array using nested for loop.
- Then close the Scanner object.
Example: Taking input of Two dimenstional array from the user.
Java
// Java program to take 2D array input from the user
// Using Scanner class and loops
import java.util.Scanner;
public class Geeks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt the user for the dimensions of the array
System.out.print("Enter the number of rows: ");
int r = sc.nextInt();
System.out.print("Enter the number of columns: ");
int c = sc.nextInt();
// Create the 2D array
int[][] arr = new int[r][c];
// Prompt the user to enter values for the array
System.out.println("Enter the elements of the array:");
// Input loop to populate the array
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
arr[i][j] = sc.nextInt();
}
}
System.out.println("The entered 2D array is:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(arr[i][j] + " ");
}
// Move to the next line for the next row
System.out.println();
}
// Close the Scanner object
sc.close();
}
}
Output:

Explanation: In this example, first we use the Scanner class to accept user input of the number of rows and columns to create a 2D array. Then, we use a for loop to accept user input and save it in the 2D array. Finally, we use another for loop to print the array’s elements.
We can use the BufferedReader and InputStreamReader classes to read user input in Java and use the IOException class to handle exceptions in input.
In the below example, we use the BufferedReader object to read user input and manage the input classes that even with the wrong input type the error is not thrown.
Approach:
- Initialize the important packages in the program mentioned in the below code such as BufferedRead, IOException and InputStremReader from java.io.
- In the method where we use the BufferedReader and InputStreadReader throws the IOException
- Then initialize the object of InputStreamReader and pass the System.in in the constructor.
- Then create the object of BufferedReader and pass the InputStreamReader object in the constructor.
- Take the size of the array from the user. The BufferedReader read the String and we use the ParseInt() method so if the user enter other than the integer number then it will throw exception so handle it using try catch.
- Then take the input of the array elements from the user and print it using the loop
- In the end close the BufferedReader object.
Example: Taking input of an array from user using BufferedReader and InputStreamReader.
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Geeks
{
public static void main(String[] args) throws IOException
{
// Initializing the object InputStreamReade
InputStreamReader in = new InputStreamReader(System.in);
// Initializing the object of BufferedReader
BufferedReader br = new BufferedReader(in);
// Take the array size from the user
System.out.println("Enter the size of the array: ");
int s = 0;
try {
s = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input for array size. Please enter a valid integer.");
return;
}
// Initialize the array's
// size using user input
int[] arr = new int[s];
// Take user elements for the array
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < s; i++) {
try {
arr[i] = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input for array element. Please enter a valid integer.");
return;
}
}
System.out.println("The elements of the array are: ");
for (int i = 0; i < s; i++) {
System.out.print(arr[i] + " ");
}
// Close the BufferedReader
br.close();
}
}
Output:

Explanation: In this example, we use BufferedReader and InputStreamReader classes to capture user input and construct an array based on the specified size. We make an array arr with the size s. We handle potential input errors with try-catch blocks. Then, we initialize and populate an array with user input, and use a loop to print its elements.
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build applications across platformsâfrom web and mobile apps to enterprise software. It is known for its Write Once, Run Anywhere capability, meaning code written in Java can run on any device that supports the Java Virtual Machine (
11 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable. The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
14 min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runt
10 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface: The interface in Java is a mechanism to
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs. Java is one of the most popular programming languages today because of it
8 min read