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"); } }
5 Réponses
+ 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.
+ 17
Becz your array only stores 3 value And you are trying to add four values.
So Change value 3 to 4.
+ 4
Good job! 👍
+ 1
thank you it works
0
it showing error /by zero