What is error in it??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is error in it???

mport java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("Enter the length of array:"); Scanner scan = new Scanner(System.in); int len = scan.nextInt(); int[] arr={}; for(int i=0;i<=(len-1);i++) { System.out.print("Enter the numbers one by one:"); int a = scan.nextInt(); arr[i]=a; } System.out.print(arr); } }

2nd Feb 2022, 6:00 AM
Ravi King
2 Answers
+ 3
1. You're missing the i at the beginning of import. 2. You aren't initializing the array correctly. Try; int[] arr = new int[len]; 3. System.out.print(arr); Will output the memory location of the the array. Use; for (int n : arr) { System.out.println(n); } To output each element of the array instead. Or if you want to output so it looks like the array. System.out.println(Arrays.toString(arr)); Note: you'll need the import; import java.util.Arrays;
2nd Feb 2022, 6:13 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
in addition to what ChaoticDawg mentioned, if you need a resizable Array, use ArrayList instead of primitive array data type.
2nd Feb 2022, 6:25 AM
Bahhaⵣ
Bahhaⵣ - avatar