Question

Write a method in java named isValidEmail that takes a string as input parameter, and returns...

Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:

 user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter

 domain represents a sequence of alphanumeric characters (i.e., letters or digits) whose length is between 1 and 12 (inclusive), but the first character must be a letter

 Exactly one character “@” separates the user name from the domain name

 ext is a sequence of lower case letters only whose length is between 1 and 3 (inclusive)

Examples: isValidEmail ("[email protected]") returns true isValidEmail ("[email protected]") returns false

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*******************************EmailValidation.java********************************/


public class EmailValidation {

   public static void main(String[] args) {

       System.out.println(isValidEmail("[email protected]"));
       System.out.println(isValidEmail("[email protected]"));
   }

   public static boolean isValidEmail(String email) {

       String username = email.split("@")[0];

       String namespace = email.split("@")[1];

       String domain = namespace.split("\\.")[0];

       String ext = namespace.split("\\.")[1];

       boolean validUsername = false; // true - no counter examples so far
       boolean validDomain = false;
       boolean validExt = false;
       for (int i = 0; i <= username.length() - 1; i++) {
           if (Character.isLetterOrDigit(username.charAt(i)) || username.charAt(i) == '_') {

               validUsername = true;
           }
       }
       for (int i = 0; i < domain.length() - 1; i++) {

           if (Character.isLetterOrDigit(domain.charAt(i)) && domain.length() <= 12) {

               validDomain = true;
           }
       }
       for (int i = 0; i < ext.length() - 1; i++) {

           if (Character.isAlphabetic(ext.charAt(0)) && ext.length() == 3) {

               validExt = true;
           }
       }
       if (email.contains("@") && email.contains(".")) {

           if (Character.isAlphabetic(username.charAt(0)) && username.length() <= 10 && validUsername && validDomain
                   && validExt) {

               return true;
           }

       }

       return false;
   }
}
/*******************output**********************/

true
false

Thanks a lot, Please let me know if you have any problem.............

Add a comment
Know the answer?
Add Answer to:
Write a method in java named isValidEmail that takes a string as input parameter, and returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can you use the isspace() method in the code what do i add to the code...

    can you use the isspace() method in the code what do i add to the code if i want to make sure that extra spaces dont affect the output of the code elect sumatra medium roast VS sumatra medium roast Det dat HETTI CV Surface Providesearch coffee record description to search for at that it was not found in the file the first on has another space in it Table 8-1 Some string testing methods Method Description isalnum() Returns true...

  • project-8a Write a function named count_letters that takes as a parameter a string and returns a...

    project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...

  • 1) One of functionalities of a compiler is to determine if variable names are correct. Here...

    1) One of functionalities of a compiler is to determine if variable names are correct. Here is the variable naming convention of Java programming language. “Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". White space is not permitted. Subsequent characters may be letters, digits, dollar signs, or underscore characters.” Also, you can assume that...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if...

    C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if the string contains the digits in descending order. The function can ignore (e.g. skip checking) all the characters that are not digits in the string. Empty string will return false. For example, string of “95421” or “862” or “8622” or “88” or “9” will return true and string of “95423” or “889” or “9445” or “449” or “” will return false.

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March","April", "May", "June", "July", "August", "September", days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day number...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT