Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description: Write a method that validates a user email format. The emai

ID: 3585383 • Letter: P

Question

Problem Description: Write a method that validates a user email format. The email format is as follows: username@***.*** To validate the email format:

First, you need to check whether the email contains ‘@’ and ‘.’ characters in the right order

Second, you need to ensure that the username (i.e., substring before @ character) consists of alphanumeric characters (a-z/A-Z, 0-9) only (i.e., combination of letters and/or digits, NO special characters)

Use the following method header: public static void validateEmailFormat(String email)

For example, validateEmailFormat(“moaabdel@iu.edu”) will display “Correct Email Address” and validateEmailFormat(“mo-aabdel@iu.edu”) will display “The username should include only letters and/or digits”

Write a test program that prompts the user to enter an email (e.g., kevin@yahoo.com) and call the validateEmailFormat method you created to validate the format of the email entered. The program should display “Correct email format” for correct emails and appropriate messages otherwise as illustrated in sample runs below: Here are sample runs of the program:

Sample 1: Enter an email account: moaabdeliu.edu @ character is missing

Sample 2: Enter an email account: moaabdel@iuedu . character is missing

Sample 3: Enter an email account: moaabdel.iu@edu @ should appear before . 2

Sample 4: Enter an email account: moaabdel-2017@iu.edu The username should include only letters and/or digits

Sample 5: Enter an email account: moaabdel@iu.edu Correct Email Address

Sample 6: Enter an email account: moaabdel2017@iu.edu Correct Email Address

Explanation / Answer

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class EmailValidation {

public static String emailREGEX="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"

+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"; //regular expression for validating email address

public static String usernameREGEX="^[A-Z0-9.@]+$"; //regular expression for validating user name

static Pattern patt;

static Matcher emailMatcher;

static Matcher usernameMatcher;

static Pattern usernamePattern;

public static void validEmailFormat(String email) {

  

patt=Pattern.compile(emailREGEX);

emailMatcher=patt.matcher(email);

usernamePattern=Pattern.compile(usernameREGEX);

usernameMatcher=usernamePattern.matcher(email.substring(0,email.lastIndexOf("@")));

  

if(email.charAt(email.length()-1)=='.' || email.charAt(email.length()-1)=='@')

{

System.out.println("character is mising");

}

else if(emailMatcher.matches()) //condition to validate email address

{

System.out.println("Valid email address ::");

}

  

else if(!usernameMatcher.matches()) //if condition to check validity of user name

System.out.println("The username should include only letters and/or digits");

else if(email.lastIndexOf('.')<email.lastIndexOf('@'))

{

System.out.println("@ should appear before .");

}

else if(email.indexOf('.')==-1 || email.indexOf('@')==-1)

{

System.out.println("character is mising");

}

  

else

{

   System.out.println("Correct Email Address");

}

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("enter email address");

String emailAddress=sc.next();

validEmailFormat(emailAddress);

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote