How to execute this code it showing error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at detyra1.mai | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to execute this code it showing error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at detyra1.mai

public static void main(String Args[]) { int[] array = new int[3]; for (int i=0; i<4; i++) { array[i] = 5+i; } array[0] = 2/0; for(int i=0; i<3;i++) { System.out.println(array); } System.out.println("Ekzekutimi nuk do te arrin deri ketu"); } }

11th Apr 2019, 9:28 AM
Yll Krasniqi
Yll Krasniqi - avatar
5 Answers
+ 4
1. The first loop limit was not within the range of array bounds (index 0 ~ 2), change this line: for(int i = 0; i < 4; i++) into for(int i = 0; i < 3; i++) 2. You are dividing by zero at this line, I don't know what you want so I can't suggest anything but just don't divide number with zero. array[0] = 2 / 0; 3. In your second loop, you are printing the array without any index reference, so you get raw string representation of the array. Change this line: System.out.println(array); into System.out.println(array[i]); Make the fix and let me know how it goes.
11th Apr 2019, 10:37 AM
Ipang
+ 17
Becz your array only stores 3 value And you are trying to add four values. So Change value 3 to 4.
11th Apr 2019, 10:28 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 4
Good job! 👍
11th Apr 2019, 4:10 PM
Ipang
+ 1
thank you it works
11th Apr 2019, 12:14 PM
Yll Krasniqi
Yll Krasniqi - avatar
0
it showing error /by zero
11th Apr 2019, 11:13 AM
Yll Krasniqi
Yll Krasniqi - avatar