-2

I want to add a custom exception to validate the name.That is name should only accepts [a-zA-Z]. If the name is invalid, user need to enter again. Help me to implement this.

public class EmployeeInfo {
static int next_id = 0;
int id;
static String name;
Date DoB, DoJ;

public void setName(final String name) {
    this.id = ++EmployeeInfo1.next_id;
    this.name = name;
}

public void setDateOfBirth(final Date DoB) {
    this.DoB = DoB;
}

public void setDateOfJoining(final Date DoJ) {
    this.DoJ = DoJ;
}

void print() {
    System.out.println("User ID: " + id);
    System.out.println("Name: " + name);
    System.out.println("Date Of Birth: " + DoB);
    System.out.println("Date of Joining: " + DoJ);
}

public static Date checkDate(final String dt) throws ParseException {

    final SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
    final SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
    final SimpleDateFormat sdf3 = new SimpleDateFormat("dd MMMM yyyy");

    Date date = null;

    try {
        date = (Date) sdf1.parse(dt);
    } catch (final ParseException e) {

        try {
            date = (Date) sdf2.parse(dt);
        } catch (final ParseException e1) {

            try {
                date = (Date) sdf3.parse(dt);
            } catch (final ParseException e2) {

                final String invalid = "Invalid Date Format,Re-enter!";
                System.out.println(invalid);
            }
        }
    }
    return date;
}




public static void main(final String[] args) throws ParseException {
    final Scanner scanner = new Scanner(System.in);
    final EmployeeInfo1 e = new EmployeeInfo1();
    System.out.println("Enter the name: ");
    e.setName(scanner.nextLine());
    Date d = null;
    while (d == null) {
        System.out.println("Enter the Date Of Birth: ");
        d = checkDate(scanner.nextLine());
    }
    e.setDateOfBirth(d);
    d = null;
    while (d == null) {
        System.out.println("Enter the Date of Joining: ");
        d = checkDate(scanner.nextLine());
    }
    e.setDateOfJoining(d);

    e.print();

}

}
Tippu
  • 11
  • 5
  • What sort of help are you expecting exactly? – Thiagz Aug 09 '17 at 06:03
  • help me to add a custom exception .. – Tippu Aug 09 '17 at 06:04
  • Create a custom exception class like: class AlsCustomException extends Exception { public AlsCustomException(String message) { super(message); } } – Fady Saad Aug 09 '17 at 06:05
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Aug 09 '17 at 06:06

4 Answers4

1

you can define your own exception by creating class extends Exception class

class MyCustomException extends Exception
{
  public MyCustomException(String message)
  {
    super(message);
  }
}

public void setName(final String name) throws MyCustomException {
    Pattern r = Pattern.compile("[a-zA-Z]+");
    Matcher m = r.matcher(line);
    if(!m.find())
        throw new MyCustomException("Name Not Valid");

    this.id = ++EmployeeInfo1.next_id;
    this.name = name;
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

You may try org.apache.common.lang.StringUtils class for validation:

 System.out.println("Enter the name: ");
String name = scanner.nextLine();
if(!StringUtils.isAlphaSpace(name)){
   throw new MyException("Name is not valid");
}else{
  e.setName(name);
}

public class MyException extends Exception{
 public MyException (String message){
super(message);
}

}
Chetan chadha
  • 558
  • 1
  • 4
  • 19
0

You can refer to this link on how to create custom exception class

How to create a custom exception type in Java?

Thiagz
  • 121
  • 1
  • 13
0

You can create your own custom exception class and extend "Exception" class. Refer this link for more details on creating custom exception

Ahi
  • 175
  • 1
  • 13