In this program user will enter an Email-id and our program will check the validity of the entered email-id. Firstly we have to decide which are the pointes to check while email-id is checked for validity.
Points to be checked for validity
The email-id should have a @ symbol
The email-id should have a ‘.’ (Dot)
The email-id should not have any space
The right part of the email-id, after dot should not have any digit
Codes of the program
import java.io.*;
class Email
{
String str,str1;
boolean bool=true;
int i,len;
char ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
while(true)
{
System.out.println("Enter the mail id:");
str=br.readLine().trim();
i=str.indexOf('@');
if(i==-1)
{
System.out.println("Invalid Email Id ('@' symbol missing).");
continue;
}
i=str.indexOf(' ');
if(i!=-1)
{
System.out.println("Invalid Email Id (space is not allowed).");
continue;
}
i=str.indexOf('.');
if(i==-1)
{
System.out.println("Invalid Email Id ('.' missing).");
continue;
}
str1=str.substring(i+1);
len=str1.length();
for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if(Character.isDigit(ch))
break;
}
if(i!=len)
{
System.out.println("Invalid Email Id (Digit is not permitted in extension part).");
continue;
}
bool=false;
}
System.out.println("Email Id="+str);
}
public static void main(String args[]) throws Exception
{
Email ob=new Email();
ob.take();
}
}
Technical analysis of the email-id validity checking program
The email-id is stored in string object ‘str’ and a series of checking is done. All these process is done from within a loop body. indexOf () function searches a character or string from any invoking string string and returns the 1st occurance location of the character or string in the invoking string if available otherwise returns -1. Using this function the special charaters are checked from the email-id and if not found, the program takes the email-id again from the user. Lastly the extension part of the email-id is checked for existence of digit in it as no digit is permitted in this part. If all these checking paoints are are passed, the email-id is a valid one otherwise there is re-entering option in this program.
while(bool==true)
ReplyDeleteThis is needed
Actually I have used an infinite loop here, So there is no need to write 'while(bool==true)'. My mistake is that I have declared and initialised the variable 'bool' but not used it in the program.
ReplyDelete