Introduction
Validating email addresses is a common requirement in software development. Ensuring that a given email is formatted correctly helps to prevent errors during registration, communication, and data storage. An email address typically follows a specific structure, such as: local-part@domain, where the local part can include letters, numbers, and symbols, while the domain part consists of a domain name and a domain extension (e.g., “.com”).
In this example, we will write a Java program that validates whether a given string is a properly formatted email address. The program will use regular expressions to check if the input matches the required email format.
Objective
The objective of this program is to create a Java-based email validator that checks if a given string is in a valid email format. The program will use regular expressions to validate the structure of the email address.
Java Program Code
public class EmailValidator { // Method to validate the email address public static boolean isValidEmail(String email) { // Regular expression pattern for validating email addresses String emailPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; // Check if the email matches the regular expression pattern return email.matches(emailPattern); } public static void main(String[] args) { // Test cases for validating email addresses String[] testEmails = { "valid.email@example.com", "invalid-email.com", "another.valid_123@mail.co", "invalid@domain..com" }; // Validate each email for (String email : testEmails) { if (isValidEmail(email)) { System.out.println(email + " is a valid email address."); } else { System.out.println(email + " is an invalid email address."); } } } }
Explanation of the Program Structure
The Java program consists of the following parts:
- isValidEmail() method: This method takes a string input (the email) and checks whether it matches the regular expression pattern defined for valid email addresses. If it does, the method returns true, indicating the email is valid.
- Regular expression (Regex): The regular expression used in the program defines the rules for a valid email. It checks for:
- Alphanumeric characters and special characters (like dots, underscores, and hyphens) in the local part.
- An ‘@’ symbol separating the local part and domain part.
- Alphanumeric characters and hyphens in the domain part, followed by a valid domain extension.
- main() method: The main method runs some test cases and validates each email address using the isValidEmail() method. It then prints whether the email is valid or invalid.
How to Run the Program
To run this Java program, follow these steps:
-
- Ensure you have Java installed on your machine. You can download it from the official website if necessary.
- Save the code above in a file named EmailValidator.java.
- Open a command prompt or terminal window, navigate to the folder where the file is saved, and compile the code using the command:
javac EmailValidator.java
-
- Once the program compiles successfully, run it using the following command:
java EmailValidator
- The program will output whether each email address in the test cases is valid or invalid.
https://virtual-local-numbers.com/countries/1190-saudi-arabia-did-numbers.html
https://babu88-in.com/casino/
I consider, that you are not right. I am assured. I can prove it. Write to me in PM, we will discuss.
It no more than reserve
I can consult you on this question. Together we can find the decision.
The phrase is removed
I can not with you will disagree.
0.о Долго я ждал
What phrase… super, a brilliant idea
https://autodoctor.od.ua/e107_plugins/forum/forum_viewtopic.php?4090.last
https://www.designspiration.com/winnitalycom/
https://1xbetnplogin.com/
https://1xbetvnlogin.com/
https://www.beentothemovies.com/2025/03/gambling-on-big-screen-lesser-known.html
https://dissertation-now.com/coursework/
https://pq.hosting/news/novosti-rossija-i-niderlandy
https://moviemad.com.in/1xbet-register-india-guide-how-to-sign-up-in-2025/
https://www.tripadvisor.co.uk/Profile/mooref54?fid=c11f6e3b-f67f-4b09-91a0-cff13f5d3c8a
Guide to Burj Khalifa Tickets
https://500px.com/p/roscarcz