How To Take Array Input From User In Java
Last Updated : Mar 11, 2024
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(System.in); 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)); } }
- 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.
- The user is prompted for the array's size before the programme reads in each element of the array one at a time.
- The Arrays.toString() method is then used to produce the contents of the array.
- We import the classes Arrays and Scanner from the java.util package at the beginning of the code.
- The toString() function used in the programme is one of the methods available in the Arrays class for working with arrays.
- The Scanner class offers ways to read input from users.
- The program's entry point, the main() method, is then used.
- The main() method is the first to be invoked when the programme is run.
- The main() method then uses the System.out.println() method to ask the user to input the array's size.
- The user input is then read in using the s.nextInt() function of the Scanner class.
- Then, using the syntax "int myArray[] = new int[size];", a new integer array called myArray is created using the array's size.
- Then, using another System.out.println() instruction, the programme asks the user to enter every element of the array individually.
- 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.
- "myArray[i] = s.nextInt();" is then used to assign the integer value to the corresponding array element.
- 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.
- "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.