All TalkersCode Topics

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

How To Take Array Input From User In Java

Last Updated : Mar 11, 2024

How To Take Array Input From User In Java

In this article we will show you the solution of how to take array input from user in java, when a user inputs an array in Java, the programme initially asks them to enter the array's size. A new array is formed with the specified size once the size has been input.

The programme then uses a loop to read in each element & store it in the appropriate array index before asking the user to enter each individual array element one at a time.

The programme can then make use of the generated array as required. Now let's talk about the idea of taking user-provided array input in Java.

Step By Step Guide On How To Take Array Input From User In Java :-

import java.util.Arrays;
import java.util.Scanner;
public class PopulatingAnArray {
   public static void main(String args[]) {
      System.out.println("Put in the array's required size:: ");
      Scanner s = new Scanner(;
      int size = s.nextInt();
      int myArray[] = new int [size];
      System.out.println("Enter each each array element one by one");
      for(int i=0; i<size; i++) {
         myArray[i] = s.nextInt();
      System.out.println("The array's contents consist of: "+Arrays.toString(myArray));
  1. As you can see, we develop a Java programme that generates an integer array depending on user input and afterwards fills the array with the integers given by the user.
  2. The user is prompted for the array's size before the programme reads in each element of the array one at a time.
  3. The Arrays.toString() method is then used to produce the contents of the array.
  4. We import the classes Arrays and Scanner from the java.util package at the beginning of the code.
  5. The toString() function used in the programme is one of the methods available in the Arrays class for working with arrays.
  6. The Scanner class offers ways to read input from users.
  7. The program's entry point, the main() method, is then used.
  8. The main() method is the first to be invoked when the programme is run.
  9. The main() method then uses the System.out.println() method to ask the user to input the array's size.
  10. The user input is then read in using the s.nextInt() function of the Scanner class.
  11. Then, using the syntax "int myArray[] = new int[size];", a new integer array called myArray is created using the array's size.
  12. Then, using another System.out.println() instruction, the programme asks the user to enter every element of the array individually.
  13. Then, each integer number given by the user is read into the for loop's for loop, which iterates over every element of the array, by using the s.nextInt() method.
  14. "myArray[i] = s.nextInt();" is then used to assign the integer value to the corresponding array element.
  15. The programme then prints the result using the System.out.println() function after converting the array's contents to a string using the Arrays.toString() method.
  16. "The array's contents consist of:" is followed by the array's contents in square brackets, separated by commas, in the final string.

Conclusion :-

As a result, we have successfully learnt the Java notion of taking user-provided array input.

In addition, we discovered that the programme asks the user to specify the size of an array, generates a new integer array with the specified size, populates the array with the integer values submitted by the user, & outputs the contents of the arrays as a string.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪