Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index.
This program may generate ArrayIndexOutOfBoundsException. Use exception handling mechanisms to handle this exception. In the catch block, print the class name of the exception thrown.
Input and Output Format:
Refer to Sample Input and Output for formatting specifications.
Note: All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output 1:
Enter the number of elements in the array
3
Enter the elements in the array
20
90
4
Enter the index of the array element you want to access
2
The array element at index 2 = 4
The array element successfully accessed
Sample Input and Output 2:
Enter the number of elements in the array
3
Enter the elements in the array
20
90
4
Enter the index of the array element you want to access
6
java.lang.ArrayIndexOutOfBoundsException
Solution
import java.util.Scanner;
public class Main {
//sauravhathi
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int n = sc.nextInt();
//sauravhathi
int[] arr = new int[n];
System.out.println("Enter the elements in the array");
for (int i = 0; i < n; i++) {
//sauravhathi
arr[i] = sc.nextInt();
}
System.out.println("Enter the index of the array element you want to access");
int index = sc.nextInt();
try {
System.out.println("The array element at index " + index + " = " + arr[index]);
//sauravhathi
System.out.println("The array element successfully accessed");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("java.lang.ArrayIndexOutOfBoundsException");
}
}
}
Happy Learning – If you require any further information, feel free to contact me.