题解 | #邮箱验证#

邮箱验证

https://www.nowcoder.com/practice/8f908eeee4aa412ca88304f0acbaa1f9

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        String emailMatcher = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";

        //write your code here......
        int index1 = 0;
        int index2 = 0;
        String pastOne = "";
        String pastTwo = "";
        String pastThree = "";
        try {
            //第一部分字串
            index1 = str.indexOf("@");
            pastOne = str.substring(0, index1);

            //第二部分字串
            index2 = str.indexOf(".");
            pastTwo = str.substring(index1 + 1, index2);

            //第三部分字串
            pastThree = str.substring(index2 + 1, str.length());

            //第三部分的字串长度
            int index3 = pastThree.length() - index2 - 1;

            //第二部分的字串长度
            index2 -= (index1 + 1);

            //利用方法进行判断格式是否合法
            if (method(pastOne, index1) && method(pastTwo, index2)
                    && method(pastThree, index3)) {
                System.out.println("邮箱格式合法");
            } else {
                System.out.println("邮箱格式不合法");
            }
        } catch (Exception e) {
            System.out.println("邮箱格式不合法");
        }
    }

    public static boolean method(String str, int index) {
        boolean legal = true;
        for (int i = 0; i < index; i++) {
            //如果邮箱格式合法则不改变legal的值
            if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9') ) {
            } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
            } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
            } else {
                //不合法
                legal = false;
            }
        }
        if (index == 0) {
            return false;
        }
        return legal;
    }
}

全部评论

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务