-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndexOutOfBoundsExceptionDemo.java
More file actions
33 lines (29 loc) · 1.18 KB
/
ArrayIndexOutOfBoundsExceptionDemo.java
File metadata and controls
33 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ExceptionHandling;
/**
* the purpose of this demo is to show the OutOfBoundException for an
* Array value.
* This will show how the exception is handled and return an output to the user.
*/
public class ArrayIndexOutOfBoundsExceptionDemo {
public static void main(String[] args) {
int[] list = {1,2,3,4};
java.util.Scanner input = new java.util.Scanner(System.in);
int lastIndex = 0;
try {
System.out.println("Enter the last index number. ");
lastIndex = input.nextInt();
if(lastIndex != list.length -1){
throw new ArrayIndexOutOfBoundsException("You put the incorrect index value, try again.");
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
do{
lastIndex = input.nextInt();
if(lastIndex != 3) {
System.out.println("You put the incorrect index value, try again.");
}
}while(lastIndex != list.length -1);
}
System.out.println(list[lastIndex]);
}
}