Skip to main content

Program in Java: To accept an email address. Then check the validity of email

 Program in Java: To accept an email address. Then check the validity of email address by following the points below:
(i)The username should be at least 5 letter in length. The first character will be at least letter.
(ii)Then check the presence of @ character. It should be in the middle not at the end.
(iii)Then check the domain if they belong to .com, .org, .in


import java.util.Scanner;
class email
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter an email address:");
        String em = sc.nextLine();
        if(em.length()>4 && em.indexOf('@')!=-1)
        {
            String user = em.substring(0,em.indexOf('@'));
            if(Character.isLetter(user.charAt(0)))
            {
                String d = em.substring(em.indexOf('.')); //domain address
                if(d.equalsIgnoreCase(".com") || d.equalsIgnoreCase(".in") || d.equalsIgnoreCase(".org"))
                    System.out.println("Valid email address");
                else
                    System.out.println("Invalid email address");
            }
            else
            {
                System.out.println("Invalid email address");
                System.exit(0);
            }
        }
        else
        {
            System.out.println("Invalid email address");
            System.exit(0);
        }
    }
}

Popular posts from this blog