Verification email addresses without regular expressions or other helper classes (Java)

Source: Internet
Author: User

In the past, there was such a question in the written test. The content was similar to the question. I knew how to do it, but I couldn't write it in the White Paper. It was immediately rejected by the Technical Manager.

This is a Java class that I have time to write for simple verification of the email format. However, regular expressions are generally used for validation of these formats, so this class can be used.

Package com. test;

Public class test ...{

/***//**
* @ Param ARGs
*/
Private string email;

Public String getemail ()...{
Return email;
}

Public void setemail (string email )...{
This. Email = Email;
}

Public Boolean isvalidate (string s )...{
Boolean flag = false;
Int index;
Int end;
// Check whether @ exists. check whether there are some special characters before @ exists.
Try ...{
If (S. indexof ("@")> 0 )...{
Index = S. indexof ("@");
End = S. Length ();
System. Out. println ("@ index is:" + index + "end:" + end );
Int index_2 = S. indexof ("@", index );
// Whether multiple @ exists @
If (index_2! =-1 )...{
// @ Check whether there are any characters in front of the string, as shown in the following code. If the character string is null, you can write a special method. I won't be arrogant here.
If (index <(end-1 ))...{
// Obtain the string following @
S = S. substring (index + 1 );
Int end_2 = 0;
While (S. indexof (".")! =-1 )...{
Index_2 = S. indexof (".");
End_2 = S. Length ();
System. Out. println (". index is:" + index_2 );
// Whether a. exists
If (index_2! =-1 )...{
// Obtain the first string.
String points = S. substring (0, index_2 );
System. Out. println ("befor.:" + points );
// Judge whether the string @ to. Is null
If (! Points. Equals (""))...{
// Determine whether a string exists in the backend.
If (index_2 <(end_2-1 ))...{
S = S. substring (index_2 + 1 );
System. Out. println ("after.:" + S );
Flag = true;
}
Else ...{
Flag = false;
Break;
}
}
}
}
}
}

}
} Catch (stringindexoutofboundsexception SE )...{
Se. printstacktrace ();
Return false;
}
Return flag;
}
Public static void main (string [] ARGs )...{
// Todo auto-generated method stub
Test T = new test ();
T. setemail ("123@1.12 ");
If (T. isvalidate (T. getemail ()))...{
System. Out. println ("OK ");
}
Else ...{
System. Out. println ("false ");
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.