-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathValidIndexOfArray.java
56 lines (52 loc) · 1.25 KB
/
ValidIndexOfArray.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//a program to check whether the indexes given are valid index are not. Print "Out of Bounds" if any attempt is made to refer to an element whose index is beyond the array size , else print the element present at the index.
//The first line of input contains n , denoting the size of array.
//The second line contains n spaced integers , denoting elements of array
//The third line contains q, denoting number of queries
//Next q lines contains an integer, index, which is to be tested.
import java.util.Scanner;
class Main{
public static void main(String[] args)
{
int n;
Scanner ref = new Scanner(System.in);
n = ref.nextInt();
int i;
int a[] = new int[n];
for(i=0;i<n;i++)
{
a[i] = ref.nextInt();
}
int r = ref.nextInt();
int h = ref.nextInt();
try {
if(r>=n)
{
throw new ArrayOutBound("Out of Bounds");
}
else
System.out.println(a[r]);
}
catch(ArrayOutBound ex)
{
System.out.println(ex.getMessage());
}
try {
if(h>=n)
{
throw new ArrayOutBound("Out of Bounds");
}
else
System.out.println(a[h]);
}
catch(ArrayOutBound ex)
{
System.out.println(ex.getMessage());
}
}
}
class ArrayOutBound extends Exception{
ArrayOutBound (String msg)
{
super(msg);
}
}