Question

Write a program(JAVATPOINT) to validate email addresses. Use a loop to go over each character, and...

  1. Write a program(JAVATPOINT) to validate email addresses. Use a loop to go over each character, and find an @ sign, followed by two or more words separated by dots. Examples of valid emails are [email protected] and [email protected], and invalid ones would be moeki&vanier.college or moeki@vanier.

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

public class EmailValidator {
  
public static void main(String[] args)
{
String email1 = "[email protected]";
String email2 = "[email protected]";
String email3 = "moeki&vanier.college";
String email4 = "moeki@vanier.";
  
System.out.println("\nRESULT 1:\n-------");
checkValid(email1);
  
System.out.println("\nRESULT 2:\n-------");
checkValid(email2);
  
System.out.println("\nRESULT 3:\n-------");
checkValid(email3);
  
System.out.println("\nRESULT 4:\n-------");
checkValid(email4);
}
  
private static void checkValid(String email)
{
boolean containsAtTheRate = false;
for(int i = 0; i < email.length(); i++)
{
if(email.charAt(i) == '@')
{
containsAtTheRate = true;
break;
}
}
  
boolean containsDot = false;
if(containsAtTheRate)
{
for(int i = 0; i < email.length(); i++)
{
if(email.charAt(i) == '.')
{
containsDot = true;
break;
}
}
  
// contains dot
if(containsDot)
{
// split the email part after @ to the last and count number of words
// for email to be valid, it must contain at least 2 words, less than 2 words ==> invalid
String remPart = email.substring(email.indexOf("@") + 1);
String[] data = remPart.split("\\.");
if(data.length >= 2)
System.out.println(email + " is a valid email address.");
else
System.out.println(email + " is not a valid email address: does not contain at least 2 words after @ sign!");
}
else
System.out.println(email + " is not a valid email address: does not contain . symbol!");
}
else
System.out.println(email + " is not a valid email address: does not contain @ symbol!");
}
}

********************************************************* SCREENSHOT *******************************************************

run: RESULT 1: - - - - - - moeki@vanier.college is a valid email address. RESULT 2: - - - - - - -- moeki@vaniercollege.qc.ca

Add a comment
Know the answer?
Add Answer to:
Write a program(JAVATPOINT) to validate email addresses. Use a loop to go over each character, and...
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
  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • 2. Write a program to prepare email address with name of person, email ID. This program...

    2. Write a program to prepare email address with name of person, email ID. This program should collect number of persons with their details menstion in the above. Finally, print all the registerd preson with their details and also print selected particular person's emails. 1. Write a program to prepare departmental store records with item name, number of items. This program should collect number of items with its details menstion in the above. Finally, print all the registerd items with...

  • PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

    PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :   ...

  • In C++ write a complete and correct x++ program that generates student email addresses and prints...

    In C++ write a complete and correct x++ program that generates student email addresses and prints them to the screen based upon the data found in an input file, called students . txt. Each line of the space, followed by the student’s last name. Every email address is to be of the form [email protected]. In general, each username has four parts in this order: (i) the student; last name in lowercase letters; (ii) a number between 10- 99 generated at...

  • In this lab assignment, you'll write code that parses an email address and formats the ci and zip...

    Using Microsoft Visual Studio C# In this lab assignment, you'll write code that parses an email address and formats the ci and zip code portion of an address. String Handling Email: [email protected] Parse City: Fresno State: ca Zp code: 93722 Format ให้ 2 Parsed String Formatted String User name: anne Domain name: murach.comm City, State, Zip: Fresno, CA 93722 OK OK Create a new Windows Forms Application named StringHandling and build the form as shown above. 1. Add code to...

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default val...

    C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default value, so that the user can simply press ENTER to accept any default. (That means that string will be the best choice of data type for the console input for each option.) The two console inputs are the names of the input and output files. The default filenames are to be fileContainingEmails.txt for the input file, and...

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