+ 1

I am taking values and store in array whats wrong with my code

public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[]=sc.nextInt(); } for(int n:a) { System.out.println (n); } } }

13th Jan 2017, 10:31 AM
harish tati
2 Réponses
+ 1
1. you are declaring n twice... I think that's not possible 2. if you want to assign a value to an array, you have to know what element you want to set. So you have to put an index into the brackets of a[]... so the line "a[] = sc.nextInt();" is wrong. In your case you should put i into the brackets and use another letter for the variable in your second loop.
13th Jan 2017, 12:05 PM
FreakManMega
0
please give correct code
13th Jan 2017, 12:08 PM
harish tati