-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionrollno.java
More file actions
44 lines (37 loc) · 1.24 KB
/
Copy pathExceptionrollno.java
File metadata and controls
44 lines (37 loc) · 1.24 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
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
public class Exceptionrollno {
public static void main(String[] args) {
int l, i, j;
Scanner s = new Scanner(System.in);
System.out.println("Enter the length:");
l = s.nextInt();
int a[] = new int[l];
for (i = 0; i < l; i++) {
System.out.println("Enter the roll no " + (i + 1) + ":");
a[i] = s.nextInt();
try {
if (a[i] > l) {
i--;
throw new ArrayIndexOutOfBoundsException("Enter the valid value");
} else {
for (j = 0; j < i; j++)
{
if (a[i] == a[j]) {
i--;
throw new Exception("Already exist");
}
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
for(int k=0;k<l;k++)
{
System.out.println("Roll no"+(k+1)+":"+a[k]);
}
}
}