0
How to read elements into an array from users input?
4 Réponses
+ 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();
}
+ 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
0
Are trying store multiple characters of a string, multiple strings, or multiple numbers into an array?
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.