Question: I want to validate the user input before the program continued. For example:

A variable String name. The program displays 'Enter name' and if the user types in a number instead of a String, a message should pop up and also make the user assign a String to name.

This is what I have so far:

```
System.out.println("Enter name");
String name = input.nextLine();

```

I tried try/catch but that did not work. I tried using 

```
  if(input.hasNextInt()){System.out.println("Type in a string!");}

```

but that carries on through the program and still assigns a number to 'name'. It does not give the user a second chance to assign a string to 'name'

Answer: 
Here is something to get you started using regular expression. It only checks for a `String` with characters between `A-Z` and `a-z`. See what happens when you try and enter "FirstName LastName" and see if you can figure out how to fix it. 

```
Scanner input = new Scanner(System.in);
String name;
while(true){
    System.out.println("Enter name");
    name = input.nextLine();
    if (name.matches("[A-Za-z]+")){
        break;
    }else{
        System.out.println("Please enter only letters");
    }
}
System.out.println("Name selected: "+name);

```

The plus sign at the end of the brackets checks to see if you have *at least* one character. So if you enter a blank name, it will go to the `else`.
Try this solution that validate the input with pop up, when user inputs something invalid.

```
public class Test {

static String name;

public static void main(String[] args) {
    final JFrame parent = new JFrame();

    parent.pack();
    parent.setVisible(false);
    name = JOptionPane.showInputDialog(parent,
            "Enter name", null);

    while (true) {
        if (name.matches("[A-Za-z]+")) {
            break;
        } else {
             name = JOptionPane.showInputDialog(parent,
                    "Please enter only letters", null);
            if (name.matches("[A-Za-z]+"))
                break;

        }
    }
    System.out.println("Name selected: " + name);
  }

}

```