58,454
社区成员
发帖
与我相关
我的任务
分享
public boolean isValidEmail(String email){
if(email != null
&& email.indexOf("@") > 0
&& (email.lastIndexOf("@") - email.indexOf("@")) == 0
&& (email.lastIndexOf(".") - email.indexOf("@")) > 1
){
return true;
}
return false;
}
public class MailTest {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入邮箱:");
String mail = sc.next();
int count=0;
for (int i = 0; i < mail.length(); i++) {
char chat = mail.charAt(i);
if (chat=='@') {
count++;
}
if (count>1) {
System.out.println("格式错误 存在多个@");
System.exit(0);
}
}
//最后出现.的位置
int pointIndex=mail.lastIndexOf(".");
int aindex=mail.indexOf("@");
if (pointIndex<aindex) {
System.out.println("格式错误,@后没有.");
System.exit(0);
}
if (aindex+1>=pointIndex) {
System.out.println("格式错误,@和.之前没字符");
System.exit(0);
}
if (aindex==0) {
System.out.println("格式错误,@在第一位");
System.exit(0);
}
System.out.println(mail+"格式正确");
}
}