+ 1
I guess you are trying to create empty int array and then asking user to enter value in array. { Scanner sc = new Scanner (System.in); int [] name = new int [6]; for(int x: name){ name[x] = sc.nextInt(); } }
7th Mar 2022, 5:10 PM
kev_coding
kev_coding - avatar
+ 1
Can't assign array elements' values by using enhanced for...loop. You'd probably have to use regular for...loop and index to assign array elements' values.
7th Mar 2022, 5:25 PM
Ipang
0
Try something like this: import java.util.Scanner ; public class Program { public static void main(String[] args) { int n; Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); //reading the number of elements from the that we want to enter n=sc.nextInt(); //creates an array in the memory of length 10 int[] array = new int[10]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { //reading array elements from the user array[i]=sc.nextInt(); } System.out.println("Array elements are: "); // accessing array elements using the for loop for (int i=0; i<n; i++) { System.out.println(array[i]); } } } //thanks to https://www.javatpoint.com/how-to-take-array-input-in-java
7th Mar 2022, 5:11 PM
The_Fox
The_Fox - avatar