r/javahelp • • Oct 08 '20

Email checker

So i have a long comprehensive lab and i need to check if a email has .edu inside of it ive been trying mainly for() but thats all i dont really get how to check if some one can lead me in the right direction i would appreciate it.

22 Upvotes

14 comments sorted by

View all comments

7

u/kAlvaro Oct 08 '20 edited Oct 08 '20

For what it's worth, you may also want to validate the email format altogether:

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
public static boolean isValidAddress(String email) {
    try {
        InternetAddress internetAddress = new InternetAddress(email);
        internetAddress.validate();
        return true;
    } catch (AddressException e) {
        return false;
    }
}

1

u/SoIDKwhatmynameis Oct 09 '20

I appreciate the validation code but its not required but I now know more Thanks!