Java Program For Email Validation And Separating Email Id From Email Server Name

Ram Pothuraju
//file name--> Email.java

import java.io.*;
import java.util.*;

class Email{
 public static void main(String args[])
 {
  Scanner input=new Scanner(System.in);
  String mail;
  boolean flag1=false,flag2=false;
 
  System.out.println("Enter Email Address:");
  mail=input.next();
  char emailid[]=mail.toCharArray();
 
  char server[]=new char[15];
  char id[]=new char[25];
  int i=0,j=0;
  for(char temp: emailid){
 
   if(temp=='@')
    flag1=true;
   
   if(flag1)
   {  
    if(temp=='.')
     flag2=true;
   }
   
   if(!flag1)
   {
    id[i++]=temp;
   }
   else
   {
    if(temp!='@')
     server[j++]=temp;
   }
  }
 
  if(flag1 && flag2)
  {
   System.out.println("\nOutput:It is valid address.");
   System.out.print("Email id:");
   
   for(char temp: id){
    System.out.print(temp);
   }
   
   System.out.print("\nEmail server address:");
   for(char temp: server){
    System.out.print(temp);
   }
  }
  else
  {
   System.out.println("\nOutput:Invalid Email Address");
  }
 }
 
}


OUTPUT

javac Email.java

java Email
Enter Email Address:
anand.patel1991@gmail.com

Output:It is valid address.
Email id:anand.patel1991
Email server address:gmail.com

java Email
Enter Email Address:
anand.patel1991@gmail

Output:Invalid Email Address

Post a Comment

0Comments

Post a Comment (0)