Student类:
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Date;
/**
* /**
* 从控制台输入用户的信息并校验
* 用正则表达式校验用户输入的信息,如果符合则放到相应的属性,如果不符合则让用户重新输入该字段
* @author Administrator
*
*/
public class Student {
private static String name; //1-5个中文汉字
private static int age; //1-100
private Date birthday; //输入格式2010/09/09
private static String email; //
private static double money; //余额,必须有两位小数
private static String computerIP; //例如192.168.36.36
public Student() {
super();
}
public Student(String name, int age, Date birthday, String email, double money, String computerIP) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
this.email = email;
this.money = money;
this.computerIP = computerIP;
}
public static void isChinese(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = sc.nextLine();
String regEx = "[\u4e00-\u9fa5]{1,5}";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(name);
boolean flg = m.matches();
if (flg == false) {
System.out.println("用户名格式有误,请重新输入!");
System.out.println("请重新输入用户名:");
name = sc.nextLine();
}
System.out.println("用户名格式正确");
}
public static void isAge(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
String age1 = Integer.toString(age);
age1 = sc.nextLine();
Pattern p = Pattern.compile("^([1-9]|[0-9]{2}|100)$");
Matcher m = p.matcher(age1);
boolean flg = m.matches();
if (flg == false) {
System.out.println("年龄格式有误,请重新输入!");
System.out.println("请重新输入年龄:");
age1 = sc.nextLine();
}
System.out.println("年龄格式正确");
}
public static void isbirthday(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入生日:");
String birthday = sc.nextLine();
Pattern p = Pattern.compile("\\d{4}/\\d{2}/\\d{2}");
Matcher m = p.matcher(birthday);
boolean flg = m.matches();
if (flg == false) {
System.out.println("生日格式有误,请重新输入!");
System.out.println("请重新输入生日:");
birthday = sc.nextLine();
}
System.out.println("生日格式正确");
}
public static void isEmail(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱:");
String email = sc.nextLine();
Pattern p4 = Pattern.compile("[a-zA-Z][a-zA-Z0-9]{5,15}@\\w+\\.com");
Matcher m4 = p4.matcher(email);
boolean flg = m4.matches();
if (flg == false) {
System.out.println("邮箱格式有误,请重新输入!");
System.out.println("请重新输入邮箱:");
email = sc.nextLine();
}
System.out.println("邮箱格式正确");
}
public static void isMoney(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入余额:");
String money1 = Double.toString(money);
money1 = sc.nextLine();
Pattern p = Pattern.compile("\\d{1,}(\\.\\d{1,2})");
Matcher m = p.matcher(money1);
boolean flg = m.matches();
if (flg == false) {
System.out.println("余额格式有误,请重新输入!");
System.out.println("请重新输入余额:");
money1 = sc.nextLine();
}
System.out.println("余额格式正确");
}
public static void isIP(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入IP:");
String computerIP = sc.nextLine();
Pattern p =
Pattern.compile("^((25[0-5]|2[0-4]\\d|[1]{1}\\d{1}\\d{1}|[1-9]{1}\\d{1}|\\d{1})($|(?!\\.$)\\.)){4}$ ");
Matcher m = p.matcher(computerIP);
boolean flg = m.matches();
if (flg == false) {
System.out.println("IP格式有误,请重新输入!");
System.out.println("请重新输入IP:");
computerIP = sc.nextLine();
}
System.out.println("IP格式正确");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getComputerIP() {
return computerIP;
}
public void setComputerIP(String computerIP) {
this.computerIP = computerIP;
}
}
测试类:
public class Test01 {
public static void main(String[] args) {
Student s = new Student();
// s.isChinese();
// s.isAge();
s.isbirthday();
// s.isEmail();
// s.isMoney();
// s.isIP();
}
}