-5

Consider an email id which is being provided in the starter class in the main method.

String emailId = "Hellooo_hell@gmail.com";

THE PROBLEM DESCRIPTION:

  1. The overall length of the emailId should be >3 and <20.

  2. The emailId must include "@" followed by a minimum of 1 and maximum of 2 "." characters.

  3. The substring before "@" must contain a combination of Upper Case, Lower Case and "_"(underscore) symbols.
  4. The first letter of the emailId must be in Upper Case.

If all the above conditions are valid it must display a success message or should display an appropriate ERROR message.

This is the code which does not work how I want it to.

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("Hell_ooo@gmail.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;
    int count2 = 0;

    //Condition 1
    if((length>3 && length<20)== true)
        flag1 = true;
    else 
        flag1 = false;

    //Condition 2
    for(int i=0;i<length;i++){
        if(email.charAt(i)=='@'){
            flag2 = true;
            for(int j=i+1;j<length;j++){
                if(email.charAt(j)=='.')
                  {
                   flag3 = true;    
                   count=++count;
                  }
                else
                    flag3 = false;
            }
            if(count<1 || count>2)
             {   
                 flag4 = false;
                 //System.out.println("Invalid position of special characters");

             }
            else
                flag4 = true;

        }
        else
            flag2 = false;
    }

    //Condition 3
    if(email.matches("[a-zA-Z_]+@.*")) 
        flag5 = true;
    else
        flag5 = false;

    //Condition4
    if(Character.isUpperCase(email.charAt(0))==true)
            flag6 = true;
    else
        flag6=false;

    if(flag1==true && flag2==true && flag3==true && flag4==true && flag5==true &&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");
    }


}
}
Om Prakash
  • 33
  • 1
  • 7
  • I strongly suggest you to look into RegEx – Terence May 13 '13 at 11:22
  • 2
    Why would you want to do that? Email ID's are case insensitive: `Hellooo_hell` and `hELlOoO_HElL` are the same user ID for the purpose of email delivery. Underscore is also not a mandatory character in the username. What are you really trying to do? Are you really validating an email address? If yes, then your approach is wrong. If not, then describe your problem. – Aleks G May 13 '13 at 11:23
  • 5
    Please use a meaningful title next time. "Java Methods and Strings" doesn't say anything about your question. – Jesper May 13 '13 at 11:23
  • Also look into http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Terence May 13 '13 at 11:25
  • Edited the problem description and added the code. Please look into it. – Om Prakash May 13 '13 at 11:49

1 Answers1

2

You can use the matches method from the String class

if(emailId.matches("[a-zA-Z_]+@.*")) {
    // ok matches;
} else {
    // does not match
}
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
  • This doesn't enforce a _combination_ of uppercase, lowercase _and_ underscore characters. – Greg Kopff May 13 '13 at 11:27
  • `greg@greg.com` matches, but it doesn't contain an uppercase letter, nor does it contain an underscore. See `Aleks G` comment in the question section. – Greg Kopff May 13 '13 at 11:32
  • @GregKopff yes sure are right, well, I guess that was not the OP intention, but until he clarifies what he actually needs this code/regex for I will refrain from guessing :) – Adam Siemion May 13 '13 at 11:45
  • Edited the problem description and added the code. Please look into it. – Om Prakash May 13 '13 at 11:49