-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNameException.java
80 lines (74 loc) · 1.49 KB
/
NameException.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
class Employee
{
public String name;
public int age;
Employee(String name,int age)
{
this.name=name;
this.age=age;
}
public void display()
{
System.out.println("Employee Dails are : ");
System.out.println("Name : "+name);
System.out.println("Age : "+age);
}
}
class NameException extends Exception
{
public String toString()
{
return("Name is not correct");
}
}
class AgeException extends Exception
{ public String toString()
{
return("Age not correct");
}
}
public class Exp7prog1 {
public static void checkname(String name) throws NameException
{
char[] namearray=name.toCharArray();
for(int i=0;i<name.length();i++)
{
if(Character.isDigit(namearray[i]))
throw new NameException();
}
}
public static void checkage(int age) throws AgeException
{
if(age>50)
{
throw new AgeException();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name :");
String nameinput=sc.nextLine();
System.out.println("Enter the age : ");
int ageinput=sc.nextInt();
try
{
checkname(nameinput);
try
{
checkage(ageinput);
}
catch(AgeException e)
{
System.out.println(e.toString());
}
}
catch(NameException n)
{
System.out.println(n.toString());
}
Employee emp=new Employee(nameinput,ageinput);
emp.display();
}
}