Java 名称验证的自定义异常

Java 名称验证的自定义异常,java,validation,exception,Java,Validation,Exception,我想添加一个自定义异常来验证名称。也就是说名称应该只接受[a-zA-Z]。如果名称无效,用户需要重新输入。帮我实现这个 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

我想添加一个自定义异常来验证
名称
。也就是说
名称
应该只接受
[a-zA-Z]
。如果名称无效,用户需要重新输入。帮我实现这个

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();

}

}

您可以尝试org.apache.common.lang.StringUtils类进行验证:

 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);
}

}

您可以参考此链接了解如何创建自定义异常类


您可以通过创建类扩展来定义自己的异常
exception
class

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


您可以创建自己的自定义异常类并扩展“异常”类。

您希望得到什么样的帮助?帮助我添加自定义异常..创建一个自定义异常类,如:class AlsCustomException extends exception{public AlsCustomException(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;
}