How to read elements into an array from users input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to read elements into an array from users input?

6th Apr 2016, 7:13 AM
R Ashwin Kumar
R Ashwin Kumar - avatar
4 Answers
+ 4
Just use a loop and enter the assign the next element of the array from the Scanner input. For example: Scanner s=new Scanner(System.in); int arr[]=new int[10]; for(int i=0;i<10;i++) { arr[i]=s.nextInt(); }
17th May 2016, 2:30 PM
James Flanders
+ 1
Scanner sc = new Scanner(System.in); System.out.println("How many numbers?"); int[] inputNumbers = new int[sc.nextInt()]; for (int i = 0; i < inputNumbers.length; i++) inputNumbers[i] = sc.nextInt(); But you need to check the numbers at the input , the exception can be
27th Jul 2016, 1:16 PM
Shwarz Andrei
0
Are trying store multiple characters of a string, multiple strings, or multiple numbers into an array?
24th Jun 2016, 1:00 AM
Scylla
Scylla - avatar
0
You ask for the input and assign it to a variable, then (assume var is the user input, and myArr is the array) myArr[1]=var; so 1 position in the array is equal to the input.
24th Jun 2016, 4:18 AM
Bobby Turnip (Scye)
Bobby Turnip (Scye) - avatar