1

本质上,这个程序的想法是测试用户输入并抛出我在输入无效数据时创建的异常。例如:名称不能为空,必须全部为字母字符(无特殊或数字)。我已将其嵌入到一个 do-while 循环中,只要不输入 q 退出该循环就会继续。我正在通过扫描线读取用户输入,然后将输入的字符串发送到验证它是否符合标准的函数。如果没有,则该函数将引发我的自定义异常。一切正常,除了抛出异常时,它仍然采用该字符串并将其放入新的 Person 对象中。

如何向用户抛出异常,但要求他们重新输入姓名或年龄,直到输入正确?

do{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter person info or q to quit.");
    System.out.print("Please enter the name of this person:");
    String name = input.nextLine();
    if(name.equalsIgnoreCase("q"))
    {
        break;
    }
    try{
        isName(name);
    }catch (InvalidNameException n){
        System.out.println(n);
    }
    System.out.print("Please enter an age for this person:");
    String age = input.nextLine();
    try{
        isValidAge(age);
    }catch(InvalidAgeException a){
        System.out.println(a);
    }



public static void isName(String name) throws InvalidNameException
{
    if(name.isEmpty())
    {
        throw new InvalidNameException("You did not enter a name.");
    }
    String[] namez = name.split(" ");
    for(int i=0;i<namez.length;i++)
    {
        char[] charz = namez[i].toCharArray();
        for (char n : charz)
        {
            if(!Character.isLetter(n))
            {
                throw new InvalidNameException("You have entered an invalid name.");
            }
        }
    }
}
4

4 回答 4

2

Put a continue; in your exception handling. It will break the loop an reenters it.

于 2014-11-03T20:33:58.507 回答
0

在 do-while 循环中评估每个变量会更好。这样如果变量中有错误age就不一定要重新输入名称。

    Scanner input = new Scanner(System.in);
    String name;
    String age;
    System.out.println("Enter person info or q to quit.");
    do{                        
        System.out.print("Please enter the name of this person: ");
        name = input.nextLine();
        if(name.equalsIgnoreCase("q")) break;
        try{
            isName(name);
            break;
        }catch (InvalidNameException n){
            System.out.println(n);
            continue;
        }
    } while (true);    

    if(!name.equalsIgnoreCase("q"))
       do{           
           System.out.print("Please enter an age for this person: ");
           age = input.nextLine();
           if(age.equalsIgnoreCase("q")) break;
           try{
               isValidAge(age);
               System.out.printf("Nombre; %s\nEdad: %s",name,age);
               break;
           }catch (InvalidAgeException a){
               System.out.println(a);
               continue;
           }
        } while (true);   
于 2014-11-03T21:52:50.877 回答
0

我假设错误在于您的 isName() 方法和显示的方法循环的兼容性。它也可能在将名称设置为变量之后发生。我不能告诉你任何真正具体的事情,因为我看不到 isName 方法。

于 2014-11-03T20:46:05.080 回答
0

我知道的最简单的方法是使用正则表达式验证获得的字符串。你可以这样做:

Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
String regex = "[A-Z a-z]+(\\s)[A-Z a-z]+";
System.out.println(name.matches(regex)? "matches": "does not match");

该表达式regex用于计算由空格分隔的一系列字母字符(无数字或特殊字符)。因此,类似“Joe Smith”的内容将通过验证,但类似“Joe 123Klkjsd”的内容不会。

您可以使用此代码并在while()循环中测试输入字符串:

while(!name.matches(regex))
{
  // Prompt the user to re-enter a valid name and assign to name variable
}

像这样的东西应该工作。

于 2014-11-03T20:52:52.323 回答