validation - .contains("@") not working for validating email format in Java -


i'm typing program school has list of students including email address. part of program needs print out invalid email addresses. i've been using .contains() method seems partially work.

public static void print_invalid_emails() {     for(student s : mstudents)         {         if(s.getemailaddress().contains("@") && s.getemailaddress().contains(".") && !s.getemailaddress().contains(" "))         {             continue;         }         else         {             system.out.println(s.getemailaddress());         }          }        } 

when checking following email addresses:

john1989@gmail.com

erickson_1990@gmailcom

the_lawyer99yahoo.com

erin.black@comcast.net

it prints erickson_1990@gmailcom not the_lawyer99yahoo.com.

any idea why is?

seems work okay me.

in way can use function validate email id. try it

 public static boolean validateemail(string email) {          string email_pattern                 = "^[_a-za-z0-9-\\+]+(\\.[_a-za-z0-9-]+)*@"                 + "[a-za-z0-9-]+(\\.[a-za-z0-9]+)*(\\.[a-za-z]{2,})$";         pattern pattern = pattern.compile(email_pattern);         matcher matcher = pattern.matcher(email);         return (matcher.matches());      } 

this function returns true if email valid else return false


Comments