forked from Kaustubh72/Hackkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcepHandle.java
More file actions
45 lines (40 loc) · 1.46 KB
/
ExcepHandle.java
File metadata and controls
45 lines (40 loc) · 1.46 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
45
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
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 in = new Scanner(System.in);
System.out.println("Enter registration number and phone number");
String reg = in.nextLine();
String no = in.nextLine();
validate(reg, no);
System.out.println("Valid");
}
}