Java 第5章 练习

1.记事本的打开与关闭

注:Runtime获取当前虚拟机信息

2.年月日 时分秒

3.判断电话

4.邮箱

import java.util.Scanner;
public class EmailCheck {
    public static boolean checkEmail(String email)
    {
        String regex1 = "[a-zA-Z]+[a-zA-Z0-9_]*@[a-zA-Z0-9]+[.][a-zA-Z0-9]+";
        //字母开头,@后加字母或数字,后面加点,后面字母或数字
        String regex2 = "[a-zA-Z]+[a-zA-Z0-9_]*@[a-zA-Z0-9]+[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+";
        //..........在regex1基础上,后面加.和其他字母组成的后缀

        if(email.matches(regex1) || email.matches(regex2))
        {
            System.out.println("合法邮箱地址");
            return true;
        }
        else
        {
            System.out.println("不合法邮箱地址");
            return false;
        }
    }
    public static boolean checkabc(String email)
    {
        //判断是否包含 某字符串,判断是某个网站的邮箱
        if(email.indexOf("@abc.com") > -1)
        {
            System.out.println("abc");
            return true;
        }
        else
        {
            System.out.println("非abc");
            return false;
        }
    }
}

class Entry
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String email = input.next();
        EmailCheck.checkEmail(email);
        EmailCheck.checkabc(email);
    }
}

5.用户注册信息进行限制

import java.util.Scanner;
public class Demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请填写用户名:");
        String name = sc.next();
        //必须是6-10位字母、数字、下划线 不能以数字开头
        while(!checkName(name)) {
            System.out.println("用户名不合法,请重新输入:");
            name=sc.next();
        }

        //必须是6-20位的字母、数字、下划线
        System.out.println("请填写密码:");
        String pwd = sc.next();

        while(!checkPwd(pwd)) {
            System.out.println("密码不合法,请重新输入:");
            pwd=sc.next();
        }
        System.out.println("注册成功!");
    }

    /**
     * 用户名验证
     *  @param name
     *  @return
     */
    public static boolean checkName(String name) {
        String regExp = "^[^0-9][\\w_]{5,9}$";
        if(name.matches(regExp)) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 密码验证
     *  @param pwd
     *  @return
     */
    public static boolean checkPwd(String pwd) {
        String regExp = "^[\\w_]{6,20}$";
        if(pwd.matches(regExp)) {
            return true;
        }
        return false;
    }

}

技能点:利用正则表达式,对用户注册信息进行限制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值