我编写了一个Java程序来验证电子邮件地址,但该程序无法正常运行。 例如:每当我们将输入设为“1”(不包括双引号)时,输出为“true”,但正确的输出必须为“false”。 请帮忙,我哪里出错了。
/*This is a java program to verify email address*/
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailVerifyer {
public boolean isEmail(String email) {
Pattern p = Pattern
.compile("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*(.[a-zA-Z]{2,3})$");
Matcher m = p.matcher(email);
if (!m.matches())
return true;
else
return false;
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an email which you would like to verify for correctness: ");
String email = scanner.nextLine();
EmailVerifyer emailVerifyer = new EmailVerifyer();
System.out.println("Output: ");
System.out.println(emailVerifyer.isEmail(email));
}
}
答案 0 :(得分:0)
替换
if(!m.matches())
与
if(m.matches())
答案 1 :(得分:0)
替换
Pattern p = Pattern.compile("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*(.[a-zA-Z]{2,3})$");
与
Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");