-2

I have a question regarding Email validation with Java. I know that the right way of using it is by regular expression and patterns, but for some reason I have been asked to write it using loop. In other words, I have to verify an email using a for loop. It is easy to reach the requirements with patterns, but when it comes to loops, it's a bit confusing.

The email must have at least one letter before @ and at least one letter between @ and dot .. And at least two letters after the dot.

What I meant is how to write a loop that checks all of these requirements using for loop? Any idea?


Earlier today I have asked the question above ... Now I reached here. The only problem now is that I cannot put a minimum of two characters after the dot.

public class EmailValid {
    public static void main (String[] args) {
        System.out.print("Please Enter Your Email To Validate: ");
        Scanner input = new Scanner (System.in);
        String email  = input.nextLine();
        for (int i = 0 ;  i < email.length(); i ++) {
            if (email.indexOf("@") >= 1 ) {
                if (email.indexOf(".") >= email.indexOf("@")+2)
                    if ((email.indexOf("@")+email.indexOf(".") - email.length()) >= 1 )
                        System.out.print("Email Passed" + (email.length() - (email.indexOf("@")+email.indexOf("."))));
            }
        }
        System.out.print("\n String legnth: "+ (email.length()) + 
                "\n Index of @: " + email.indexOf("@") +
                "\n Index of dot: " + email.indexOf("."));
    }
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Whizz
  • 5
  • 5

1 Answers1

0

Better you should stick to the method which you have adopted. You should traverse the entire String using the for-loop. I think the code must be like this :-

public class EmailValid {
 public static void main (String[] args) {
 System.out.print("Please Enter Your Email To Validate: ");
 Scanner input = new Scanner (System.in);
 String email  = input.nextLine();
 boolean flag=false;
 int countr=0,countd=0;
 loop:for (int i=0; i < email.length();i++) {                 //Better to iterate loop till end of the String "email"
 if(email.charAt(i)=='@'){
  countr++;
  if(countr>1){
   flag=false;
   break loop;
  }
   if(i>=1) 
   flag=true;
   else{
   flag=false;
   break loop;
    }
  }
 if(email.charAt(i)=='.'){
  countd++;
  if(countd>1){
      flag=false;
      break loop;}
   if(i>=3)
 flag=true;
 else{
   flag=false;
   break loop;}
if(email.length()>=i+3)
  flag=true;
 else{
  flag=false;
  break loop;}
 }
 if((email.indexOf(".")-email.indexOf("@"))>=2)
{
  flag=true;}
 else {
  flag=false;
  break loop;}
if(((int)email.charAt(i))>=65 && ((int)email.charAt(i))<=90)
 flag=true;
 else if(((int)email.charAt(i))>=97 && ((int)email.charAt(i))<=122)
 flag=true;
 else if(((int)email.charAt(i))>=48 && ((int)email.charAt(i))<=57)
 flag=true;
 else if(((int)email.charAt(i))==64 || ((int)email.charAt(i))==46)
 flag=true;
 else
 flag=false;
 //@ must at least have a char before itself and also there must be a character between '@' and '.'--So mininmum index of '@' >=1 and for '.' >=3.
 if(flag==false)
break loop;
 }
 if(flag==true && email.length()>=5)    // The minimum length of the String must be equal to minimum index of '.' +2, i.e., >=5. 
  System.out.println("Validated");
  else 
  System.out.println("Invalid Email");
   }
  }
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 1
    So +=+=+=.+=+=@78 should be a valid address if I have confidence in your code ?!!! – Serge Ballesta Jun 08 '14 at 15:50
  • You still not verify that '.' comes **after** '@'. IMHO, you should use a state enum ( begin, after_arobas, after_dot), it will be cleaner and more easy to understand. But good tries ... – Serge Ballesta Jun 08 '14 at 16:05
  • @Whizz-The problem was because of no break option option after becoming false! SORRY. Please try now! – Am_I_Helpful Jun 08 '14 at 16:54
  • 2
    @shekharsuman this in now way *it is complete in all aspects like a real "E-Mail"!*, far from it! [ RFC5322](http://tools.ietf.org/html/rfc5322). –  Jun 08 '14 at 21:09
  • If this took 45 mins to get so wrong you should reconsider your profession! –  Jun 08 '14 at 21:09
  • @JarrodRoberson Any suggestions beside his answer? – Whizz Jun 08 '14 at 21:36
  • @Whizz - yes read the RFC specification for email addresses and understand it before you try to implement something that tests to see if a string is a conformant address. –  Jun 09 '14 at 02:56
  • @JarrodRoberson-Firstly, I meant to say 45 minutes because I was eating while solving his problem. I was doing both work at the same-time. Secondly, my 45 minutes meant only 10+ minutes, this 45 was to make him realise how bad is it to give home-work questions! Lastly, I am a student and still in learning phase; I don't need a change in my profession--My E-mail is in accordance with "Latin characters" allowed for E-mail, not for the Greek,Chinese,etc and neither I care for them. characters-So,I hope replied fine in all aspect. Do you agree OR I am really left with some more points to go on? – Am_I_Helpful Jun 09 '14 at 04:30
  • @JarrodRoberson-Please check the link below and tell me where it is wrong :- http://tinypic.com/view.php?pic=iojhok&s=8 It's working fine for Latin characters which he and I both wanted!!! Please share your thought. – Am_I_Helpful Jun 09 '14 at 04:44