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("."));
}
}