- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to populate an array one value at a time by taking input from user in Java?
To read data from user create a scanner class. Read the size of the array to be created from the user using nextInt() method. Create an array with the specified size. In the loop read the values from the user and store in the array created above.
Example
import java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); int myArray[] = new int [size]; System.out.println("Enter the elements of the array one by one "); for(int i=0; i<size; i++) { myArray[i] = s.nextInt(); } System.out.println("Contents of the array are: "+Arrays.toString(myArray)); } }
Output
Enter the required size of the array :: 5 Enter the elements of the array one by one 78 96 45 23 45 Contents of the array are: [78, 96, 45, 23, 45]
- Related Articles
- Taking input from the user in Tkinter
- Java Program to fill an array of characters from user input
- How to input multiple values from user in one line in Java?
- How to select one item at a time from JCheckBox in Java?
- Can we read from JOptionPane by requesting input from user in Java?
- How to input multiple values from user in one line in Python?
- How to input multiple values from user in one line in C#?
- How to populate a 2d array with random alphabetic values from a range in Java?
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- Java Program to Get Input from the User
- How to read an input value from a JTextField and add to a JList in Java?
- How to create and populate two-dimension Java array?
- Taking multiple inputs from user in Python
- Taking input from console in Python
- Adding two values at a time from an array - JavaScript

Advertisements