1

Essentially the idea of this program is to test user input and throw exceptions that I've created when invalid data is entered. For example: name cannot be empty and must be all alpha characters (no special or numeric). I have embedded this in a do-while loop that will continue so long as q is not entered to quit. I'm reading in the user input via scanner line and then sending the string inputted to a function that validates whether it meets the criteria. If it does not, then the function throws my custom exceptions. It all works fine EXCEPT when the exception is thrown it still takes that string and puts it in the new Person object.

How do I throw the exception to the user but THEN require them to re-enter the name or age until it's entered correctly?

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.");
            }
        }
    }
}
Exziled
  • 21
  • 4

4 Answers4

2

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

Aviator
  • 512
  • 3
  • 7
  • Sorry, can you explain that a little further? Having trouble understanding the logic behind it! A continue; in the catch clause? – Exziled Nov 03 '14 at 20:40
  • Correct. Within the catch clause. – Aviator Nov 03 '14 at 20:52
  • Thanks! That worked! I also had an error at the bottom of the do while where I was putting the data into the Person obj even if it was incorrect. Didn't realize that wasn't included in the try block. All works now! – Exziled Nov 03 '14 at 20:59
0

I would assume that the error lies within the compatibility of your isName() method and the method loop shown. It probably happens after it sets the name to a variable too. I cant tell you anything really specific because I cant see the isName method though.

Nicholas Eason
  • 290
  • 4
  • 13
  • The isName just takes in a string name and checks it for special characters. It has no return. Just throws new exceptions if the name is blank or contains special characters. I'll edit the original to show it at teh bottom. – Exziled Nov 03 '14 at 20:51
  • Ok, so its not within the isName() method, like I said though, I couldn't really be sure. Remember that you can also use a `finally` clause after your catch and it will execute though! Also, remember to mark Aviator's answer as correct if it worked for you! – Nicholas Eason Nov 03 '14 at 22:57
0

The easiest way I know of doing this is to validate the obtained String using a regular expression. You can do something like this:

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

The expression regex is used to evaluate a sequence of alpha characters (no numbers or special characters) separated by a space. So, something like: "Joe Smith" will pass validation, but something like "Joe 123Klkjsd" will not.

You can take this code and test the input String in a while() loop:

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

Something like that should work.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
0

It would be better to evaluate each variable within a do-while loop. Thus if there is an error in the variable age would not necessarily re-enter the name.

    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);   
SkyMaster
  • 1,323
  • 1
  • 8
  • 15