-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndexOutOfBoundsException
More file actions
35 lines (27 loc) · 1014 Bytes
/
ArrayIndexOutOfBoundsException
File metadata and controls
35 lines (27 loc) · 1014 Bytes
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
34
35
package taoshiflex.lab8;
import java.util.Random;
import java.util.Scanner;
public class Lab8 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Create an array with 100 randomly chosen integers
int[] numbers = new int[100];
Random random = new Random();
for (int i = 0; i < numbers.length; i++) {
numbers[i] = random.nextInt(1000); // Random integers between 0 and 999
}
// Prompt the user to enter an index
System.out.print("Enter the index of the array (0 to 99): ");
try {
int index = scan.nextInt();
System.out.println("Value at index " + index + ": " + numbers[index]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Out of Bounds");
}
catch (Exception e) {
System.out.println("Invalid input. Please enter a valid index.");
}
scan.close();
}
}