I'm trying to practice exception handling in java, and doing so by running a few (in)valid e-mail addresses and validating them. So far, I'm able to print out the non validated ones, but I'm looking for a way to also print the one who is validated. If I println in the try block, the john.doe.gmail.com address prints out as being validated for some reason.
Could someone suggest something?
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
public class AdressChecker {
public static void main(String args[]) {
String toTest[] =
{"john.doe@gmail.com", "john.doe.gmail.com", "john.doe@", "@gmail.com"};
System.out.println("Addresses to be verified");
for (String x : toTest)
System.out.println(x);
for (String s : toTest) {
try {
InternetAddress emailAddr = new InternetAddress(s);
emailAddr.validate();
} catch (AddressException ex) {
System.out.println(s + " is not a valid E-Mail Address.");
}
}
}
}
Working now. The output is:
Addresses to be verified
john.doe@gmail.com
john.doe.gmail.com
john.doe@
@gmail.com
john.doe@gmail.com is a valid E-Mail Address.
john.doe.gmail.com is not a valid E-Mail Address.
john.doe@ is not a valid E-Mail Address.
@gmail.com is not a valid E-Mail Address.
BUILD SUCCESSFUL (total time: 0 seconds)