title: Email validation in Java without using regular expression
body: <p>I understand that validating email with <strong>Regex</strong> would have just been a matter of 3-4 lines of code. However, I'm looking to validate email <strong><code>without using</code></strong> <strong><code>Regex.</code></strong> To an extent, the code successfully passes almost all the validations, however, still unable to figure out - how special characters can be avoided being the first &amp; last character of the email address.</p>
  
  <p><strong>List of specialChars =</strong> <code>{'!', '#', '$', '%', '^', '&amp;', '*', '(', ')', '-', '/', '~', '[', ']'} ;</code></p>
  
  <p><strong>What I am looking at is:</strong></p>
  
  <blockquote>
    <p>If the username section (<strong><code>abc.xyz</code></strong>@gmail.com) starts or ends with
    any of the special characters, it should trigger an <strong>"Invalid email
    address" error.</strong> The same goes with domain section as well.</p>
  </blockquote>
  
  <p><strong>For eg...</strong> the following list of <strong>email-IDs</strong> should print an <strong>"Invalid email ID"</strong> <strong><code>error message</code></strong></p>
  
  <blockquote>
    <p><strong><code>#abc.xyz@gmail.com</code></strong></p>
    
    <p><strong><code>abc.xyz&amp;@gmail.com</code></strong></p>
    
    <p><strong><code>abc.xyz&amp;@!gmail.com</code></strong>   </p>
    
    <p><strong><code>abc.xyz&amp;@gmail.com!</code></strong>       </p>
  </blockquote>
  
  <pre><code>import java.util.Scanner;
  
  public class Email_Validation {
  
      public static void main(String[] args) {
  
          // User-input code
          Scanner scan = new Scanner(System.in);
          System.out.println("Enter your email address");
          String email = scan.next();
  
          // Code to check if email ends with '.' (period sign) 
          boolean checkEndDot  = false;
          checkEndDot = email.endsWith(".");
  
          // Code to find out last index of '@' sign
          int indexOfAt = email.indexOf('@');
          int lastIndexOfAt = email.lastIndexOf('.');
  
  
          //Code to check occurence of @ in the email address  
          int countOfAt = 0;
  
          for (int i = 0; i &lt; email.length(); i++) {
              if(email.charAt(i)=='@')
                  countOfAt++; }
  
  
          // Code to check occurence of [period sign i..e, "."] after @ 
          String buffering = email.substring(email.indexOf('@')+1, email.length());
          int len = buffering.length();
  
          int countOfDotAfterAt = 0;
          for (int i=0; i &lt; len; i++) {
              if(buffering.charAt(i)=='.')
                  countOfDotAfterAt++; }
  
  
  // Code to print userName &amp; domainName
              String userName = email.substring(0, email.indexOf('@'));
              String domainName = email.substring(email.indexOf('@')+1, email.length());
  
                  System.out.println("\n");   
  
                 if ((countOfAt==1) &amp;&amp; (userName.endsWith(".")==false)  &amp;&amp; (countOfDotAfterAt ==1) &amp;&amp;   
                    ((indexOfAt+3) &lt;= (lastIndexOfAt) &amp;&amp; !checkEndDot)) {
  
                     System.out.println("\"Valid email address\"");}
  
                 else {       
                          System.out.println("\n\"Invalid email address\""); }
  
  
                  System.out.println("\n");
                  System.out.println("User name: " +userName+ "\n" + "Domain name: " +domainName);
  
  
      }
  }
  </code></pre>
  
  <p><strong>How do I get this resolved ?</strong> </p>