Question Given:

Prompt the user to input a name. The name may have a salutation, and valid salutations are “Mr” and “Ms”. Next, after a single whitespace character (if there was a salutation) is the first name. After another whitespace character, there may or may not be a middle initial, which is a single uppercase letter which may or may not have a “.” after it. Finally, after another whitespace, the last name is given. The first and last name will both start with uppercase letters. Determine, using a single regex check, whether or not the input name is valid. If it is valid, print the first and last name. If not, let the user know.

Example:

Program: Please enter a name:

User: Mr Ryan D Scott

Program: Ryan Scott

---

Program: Please enter a name:

User: Mr Ryan D. Scott

Program: Ryan Scott

---

Program: Please enter a name:

User: Ryan Scott

Program: Ryan Scott

---

Program: Please enter a name:

User: Mr Ryan Scott

Program: Ryan Scott

---

Program: Please enter a name:

User: Ryan D Scott

Program: Ryan Scott

---

Program: Please enter a name:

User: Captain Fluffyface the 6th 

Program: Invalid name. 

---

Program: Please enter a name:

User: Spongebob Squarepants

Program: Spongebob Squarepants

---

I need help fixing the regex expression and having it output the first and last name inputted only as shown in the examples above (updated code)

```
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;

public class lab2q2 {

  public static void main(String[] args) {      

    String RegularExp = "((Mr|Ms))?[A-Z][a-z]+([A-Z]([a-z]+\\.))?([A-Z](a-z)+)"; 
    Pattern pattern = Pattern.compile(RegularExp);
    Scanner keyboard = new Scanner(System.in);

    for(int i = 0; i< 11; i++)
    {
            System.out.println("Please enter a name: ");

            String inputString = keyboard.nextLine(); 
            Matcher matcher = pattern.matcher(inputString );
            if (!matcher.matches()) 
            {
                System.out.println("Name: " + inputString);
            }
            else
            {
                System.out.println("Invalid name.");
            }
    }

    keyboard.close(); 

   }// end method main
}// end class

```