All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Take Integer Array Input In Java Using Scanner

Last Updated : Mar 11, 2024

How To Take Integer Array Input In Java Using Scanner

In this article we will show you the solution of how to take integer array input in java using scanner, Java is a popular programming language used to develop various applications.

When we work with Java, we often need to receive input from the user in the form of an array of integers.

In this tutorial, we will learn how to access integer array in Java using Scanner.

You need to follow some simple steps to input an array of integers in Java using a scanner.

First, we will create a Scanner class object used to read user input.

Next, we will use the nextInt() method of the scanner class to read an integer from the console and store it in an array of integers.

Step By Step Guide On How To Take Integer Array Input In Java Using Scanner :-

import java.util.Scanner;
public class IntegerArrayInput {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(;
      int n = scanner.nextInt();
      int[] arr = new int[n];
      for (int i = 0; i < n; i++) {
         arr[i] = scanner.nextInt();
  1. The first step is to import the scanner class from the java.util package. This class is used to read input from the console.
  2. Next, you need to create an object of the Scanner class. We can create an object by passing as a parameter to the constructor using the new keyword. This tells the scanner object to read inputs from the console.
  3. After we create the scanner object, we need to read the size of the array of integers. We can do this by calling the nextInt() method of the Scanner class and storing the result in a variable. This variable determines the size of the array.
  4. Since an array has a size, we can create an integer array of that size. We do this by declaring an array and specifying the size we read from the console.
  5. Finally, we need to read array elements from the console. We can use this loop from 0 to n-1, where n is the size of the array. Inside the loop, we call the nextInt() method of the Scanner class and store the result at the current index of the array.
  6. After we read all the elements of the collection, we need to close the Scanner object to free the system resources. We can call this near () method Scanner class.

Conclusion :-

Integer array input in Java using Scanner is a straightforward process that involves creating an object of the Scanner class, reading the size of the array, creating the array, and then reading the elements of the array from the console.

By following the simple steps outlined in this guide, you can easily access integer arrays in Java using the scanner tool.

I hope this article on how to take integer array input in java using scanner helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪