How to Take Array Input in Java - The Coding Shala

Home >> Java Programs >> Take Array Input

 In this post, we will learn how to take an array input in Java.

How to Take Array Input in Java

Write a Java Program to take array input from the user. In this program, we are going to take only an integer array as input.

We are going to use the Scanner class to take integer inputs. The below are steps to take array input:

  1. Take the size of the array as input.
  2. Create the array of the above size.
  3. Using for loop take integer elements from the user as input.
  4. Print the array.

Java Program: 

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        System.out.println("Enter the size of array");
        int size = sc.nextInt();

        // create an array of given size
        int[] arr = new int[size];

        // take integer inputs using for loop
        System.out.println("Enter " + size + " elements");
        for (int i=0; i < size; i++) {
            arr[i] = sc.nextInt();

        // print array
        System.out.println("The Array is: ");
        for (int i=0; i < size; i++) {
            System.out.print(arr[i] + " ");


Enter the size of array
Enter 5 elements
The Array is: 
1 2 3 4 5 

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

LeetCode - Crawler Log Folder Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

First Unique Character in a String Java - The Coding Shala

Add two numbers in Scala - The Coding Shala