您的位置:首页 > 编程语言 > Java开发

java--String类操作:注册系统:验证输入的身份证、手机号、座机号是否合法

2018-05-21 20:01 447 查看
import java.util.Scanner;

/**
* 注册系统:验证输入的身份证、手机号、座机号是否合法
* @author Administrator
*
*/
public class Register2_Re{

/**
* 验证身份证
* @param id 传进来的id
* @return true 身份证合法, false 身份证不合法
*/
public boolean checkId(String id){
if (id == null) {
return false;
}
if (id.length() != 16 && id.length() != 18) {
return false;
}
return true;
}

/**
* 验证手机号
* @param cellphone 传进来的手机号
* @return true 手机号合法, false 手机号不合法
*/
public boolean checkCellPhone(String cellphone){
if (cellphone == null) {
return false;
}
if (cellphone.length() != 11) {
return false;
}
return true;
}

/**
* 验证座机号
* @param telephone 传进来的座机号
* @return true 座机号合法, false 座机号不合法
*/
public boolean checkTelephone(String telephone){
if (telephone == null) {
return false;
}
String[] strs = telephone.split("-");
if (strs.length != 2 ||
(strs[0].length() != 4 && strs[1].length() != 7)){
return false;
}
return true;

}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Register2_Re reg = new Register2_Re();

System.out.println("*******************欢迎进入注册系统*******************");
System.out.print("\n");

do {
System.out.println("请输入身份证号码:");
String id = scanner.next();

System.out.println("请输入手机号:");
String cellphone = scanner.next();

System.out.println("请输入座机号:");
String telephone = scanner.next();

boolean flag1 = reg.checkId(id);
boolean flag2 = reg.checkCellPhone(cellphone);
boolean flag3 = reg.checkTelephone(telephone);

if (!flag1) {
System.out.println("身份证号必须是16位或18位");
continue;
}
if (!flag2) {
System.out.println("手机号必须是11位");
continue;
}
if (!flag3) {
System.out.println("座机号必须是4位,电话号码必须是7位");
continue;
}

if (flag1 && flag2 && flag3) {
System.out.println("注册成功!");
break;
}

} while (true);
}
}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: