In: Computer Science
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab.
1. Prompt the user to enter the email address.
2. Read the user input as a String.
3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the String class and output the result returned by the method.
4. Prompt the user to enter the full name.
5. Full name should have First name, zero or Middle names and the Last name. For this purpose, we will assume a name (first, middle or last) is a set of characters without any spaces. Some examples are “John”, “McDonald”, “smiTh”. The names are separated by exactly one space. Write a regular expression to represent this pattern.
6. Use the matches method in the String class to validate the name entered by the user and output the result returned by the method.
A possible dialogue with the user might be;
Please enter the email address: [email protected]
true
Please enter the full name: Malaka Second Third Walpola
true
if you have any doubts, please give me comment...
import java.util.Scanner;
public class Validations{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
boolean isValid;
System.out.print("Please enter the email address: ");
String email = scnr.nextLine();
isValid = email.matches("[a-zA-Z0-9.]++@[a-zA-Z]++.(com|ca|biz)");
System.out.println(isValid);
System.out.print("Please enter the full name: ");
String fullName = scnr.nextLine();
isValid = fullName.matches("^[a-zA-Z]+(?: [a-zA-Z]+)* ?$");
System.out.println(isValid);
}
}