我正在尝试在 java 中练习异常处理,并通过运行一些(无效的)电子邮件地址并验证它们来实现。到目前为止,我可以打印出未验证的,但我正在寻找一种方法来打印已验证的。如果我在 try 块中使用 println,则 john.doe.gmail.com 地址会因某种原因打印为已验证。
有人可以提出一些建议吗?
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.");
}
}
}
}
现在工作。输出是:
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)