The program should print out values stored in an array of size 5 after the user has input the values.
Here is my code:
import java.util.Scanner;
public class Arrays_Qu1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int arr[]= new int [5];
System.out.println("Enter a number");
int i;
for (i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
}
System.out.println(arr[i]);
}
}
After I enter the 5th value, the program does not terminate but instead throws:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5