title: Java Program - Email address verification using Strings and RegEx
body: <p>The problem statement goes like this:</p>
  
  <p>We need to create a String data type called <strong>emailId</strong></p>
  
  <p>The email ID has to be set using appropriate setter methods.</p>
  
  <p>The validation rules for the email ID check have to be implemented in the main().</p>
  
  <p>Conditions are:</p>
  
  <ol>
  <li><p>Overall length of the email ID must be >3 and &lt;20.</p></li>
  <li><p>The emailId must include "@" followed by a minimum of 1 and maximum of 2 "." characters.</p></li>
  <li><p>The substring before "@" must contain a combination of Upper Case, Lower Case and "_"(underscore) symbols.</p></li>
  <li><p>The first letter of the email Id must be in Upper Case.</p></li>
  </ol>
  
  <p>If all the above conditions are valid, it should display "Email ID is valid" or else, it should display an appropriate error message</p>
  
  <p>This is my code:</p>
  
  <pre><code>public class EmailCheck {
  
  String emailId;
  public void setEmailId(String emailId){
      this.emailId=emailId;
  }
  public String getEmailId(){
      return emailId;
  }
  public static void main(String[] args) {
      EmailCheck em = new EmailCheck();
      em.setEmailId("CFDV@gm.a.il.com");
      String email = em.getEmailId();
      int length = email.length();
      boolean flag1 = false;
      boolean flag2 = false;
      boolean flag3 = false;
      boolean flag4 = false;
      boolean flag5 = false;
      boolean flag6 = false;
      boolean flag7 = false;
      int count = 0;
  
  
      //Condition 1 
      if((length&gt;3 &amp;&amp; length&lt;20)== true)
          flag1 = true;
      else 
          flag1 = false;
  
      //Condition 2
          int temp = email.length();
          if(email.contains("@")){
              flag2=true;
              int a=email.indexOf("@");
               for(int i=a;i&lt;temp;i++){
          if(email.charAt(i)=='.'){
          flag3=true; 
          count=count+1;
          }
          }
          if(count&lt;1||count&gt;2){
          flag4=false;
          }
          else{
          flag4=true;
          }
          }
          else{
          flag2 =false;
          System.out.println("No @ symbol present");
          }
  
  
      //Condition 3
      if(email.matches("[A-Z a-z _]+@.*")) //Unable to get the right RegEx here!
          flag5 = true;
      else
          flag5 = false;
  
      //Condition4
      if(Character.isUpperCase(email.charAt(0))==true)
              flag6 = true;
      else
          flag6=false;
  
      if(flag1==true &amp;&amp; flag2==true &amp;&amp; flag3==true &amp;&amp; flag4==true &amp;&amp; flag5==true &amp;&amp;flag6==true)
          System.out.println("Email ID is valid");
      else{
          if(flag1==false)
              System.out.println("Inavlid length of Email ID");
          if(flag2==false||flag3==false||flag4==false)
              System.out.println("Invalid Position of Special Characters");
          if(flag5==false)
              System.out.println("Invalid combination for username");
          if(flag6==false)
              System.out.println("Invalid case of first letter");
      }
  
  
  }
  }
  </code></pre>
  
  <p>I'm not sure of the condition #2(the logic?) and condition #3(the RegExp part). A few of the test cases seem correct, the rest of them are wrong(owing to faulty logic in #2 and esp #3, I think.) </p>
  
  <p>Please, tell me what changes have to be done here to get the right output.
  Thanks!</p>