如何用JAVA语句判断邮箱的正确格式?

这周在java课程中学习了怎样 判断邮箱地址的对错。

 //接受 用户输入的一个电子邮件地址,判断格式是否正确。

//判断内容

//1 )、必须包含‘@’和‘,’符号

//2 )、@符号必须在.符号之前

//3 )、@和.符号的前后以及中间必须有内容

下边来看看我的代码吧!

import java.util.Scanner;

public class Exercise {

                                    public static void main(String[] args) {

                                                                             System.out.println("输入邮箱地址");
                                                                              Scanner scan = new Scanner(System.in);
                                                                               String s = scan.next();
                                                                                int a = s.indexOf("@");
                                                                                int b = s.indexOf(",");
                                                                            if(a == -1 || b == -1 || a > b)
                                                                            System.out.println("输入错误");
                                                                             else if(s.indexOf("@", a+1)!=-1)

                                                                             System.out.println("输入两个@");  

                                                          else

                                                       {
                                                  System.out.println("输入正确");
                                                                   System.out.println("用户名是" + s.substring(0,a));
                                                                   System.out.println("用户名是" + s.substring(a+1,s.length()));
                                                        }
                                                 }


                                                          }

你可能感兴趣的:(java)