-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathExcepHandle.java
43 lines (37 loc) · 1.41 KB
/
ExcepHandle.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
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExcepHandle {
static void validate(String r, String n) {
if (r.length() != 9) {
System.out.println("Invalid");
throw new IllegalArgumentException("Register Number does not contain exactly 9 characters");
}
if (n.length() != 10) {
System.out.println("Invalid");
throw new IllegalArgumentException("Mobile Number does not contain exactly 10 characters");
}
String pattern = "^[6|7|8|9]{1}\\d{9}";
Pattern a = Pattern.compile(pattern);
Matcher m1 = a.matcher(n);
if (!m1.find()) {
throw new NumberFormatException("Mobile Number cannot contain any character other than a digit");
}
String pattern2 = "^[1-9]{2}[A-Z]{3}[0-9]{4}$";
Pattern b = Pattern.compile(pattern2);
Matcher m2 = b.matcher(r);
if (!m2.find()) {
throw new NoSuchElementException(
"Registration Number cannot contain any character other than digits and alphabets");
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String reg = sc.nextLine();
String no = sc.nextLine();
sc.close();
validate(reg, no);
System.out.println("Valid");
}
}