JAVA 异常处理,正则表达式的使用和匹配

1 篇文章 0 订阅
1 篇文章 0 订阅

Java的异常处理采用try catch的语句进行,相当于If else的循环语句,而throw 相当于system.out.print并且还会在加上while(true)时候还会退出程序

 

package first_experience;

import java.util.*;


import java.io.*;

import java.util.regex.*;



public class first {
	public String name;
	public String major;
	public int num;
	public String num1;
	public String keyword;
	
	public String getName(){//定义一个方法,其作用是返回String类型的name,该方法无参数
		return name;
	}
	public void setName(String name)
	throws WrongInputException
	{//定义一个方法,其作用是定义name
		for(int i=0;i<name.length();i++)
		{
			if(Character.isDigit(name.charAt(i))){//如果名字中出现数字则抛出异常
				throw new WrongInputException("名字中不能输入数字");
			}
		}
		this.name=name;//没有数字则进行赋值
	}
	
	
	public String getMajor(){//定义一个方法,其作用是返回String类型的name,该方法无参数
		return major;
	}
	public void setMajor(String major)
	throws WrongInputException
	{//定义一个方法,其作用是定义name
		for(int i=0;i<major.length();i++)
		{
			if(Character.isDigit(major.charAt(i))){//如果名字中出现数字则抛出异常
				throw new WrongInputException("专业中不能输入数字");
			}
		}
		this.major=major;//没有数字则进行赋值
	}


	public String getNum(){//定义一个方法,其作用是返回学号
		return num1;
	}
	public void setNum(String num1) throws WrongInputException{//定义一个方法,其作用是进行学号的赋值并进行错误异常判断处理
		Pattern p1 = Pattern.compile("\\D");//匹配数字的正则表达式
		//Pattern p2=Pattern.compile("\\w{11}");//匹配位数是否满足
		Pattern p3=Pattern.compile("0\\d{10}");//匹配以0开头的数据
		Matcher m1 = p1.matcher(num1);
		//Matcher m2 = p2.matcher(num1);
		Matcher m3 = p3.matcher(num1);
		if(num1.length()!=11){
			throw new WrongInputException("请输入正确的学号个数");
		}
		else if(m1.find()){
			throw new WrongInputException("请输入数字,不要输入其他字符");
		}
		else if(m3.find()){
			throw new WrongInputException("学号不能以0开头");
		}
		else{
			this.num1=num1;
		}
	}
	
	
	public String getKeyword(){
		return keyword;
	}
	public void setKeyword(String keyword) throws WrongInputException{
		Pattern p1 = Pattern.compile("\\w{8,12}");
		Pattern p2 = Pattern.compile("[A-Z]");
		Pattern p3 = Pattern.compile("[a-z]");
		Pattern p4 = Pattern.compile("\\d");
		
		Matcher m1 = p1.matcher(keyword);
		Matcher m2 = p2.matcher(keyword);
		Matcher m3 = p3.matcher(keyword);
		Matcher m4 = p4.matcher(keyword);
		if(keyword.length()<8||keyword.length()>12)
			throw new WrongInputException("输入串长度应该在8-12位间!");
		else if(!m1.matches())
			throw new WrongInputException("输入请勿包含数字和大小写字符外的其他字符");
		else if(!m2.find())
			throw new WrongInputException("输入请包含大写字符!");
		else if(!m3.find())
			throw new WrongInputException("输入请包含小写字符!");
		else if(!m4.find())
			throw new WrongInputException("输入请包含数字!");
		else{
			this.keyword=keyword;
		}
	}

	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// 创建一个输入输出类进行输入输出处理
		String tmp = "";// 初始化定义tmp为空字符
		String a, b, c;// 定义三个String类型的abc
		a = "";
		int td = 0;// 初始化定义td为零

		first t = new first();// 创建名字为first的对象,其可以调用该类中的所有方法和属性
		while (true) {

			try {
				System.out.println("请输入姓名");
				tmp = br.readLine();// 对数据进行读入,以回车键作为判断数据完全读入的标志,并且将读入的数据赋值给string类型的tmp
				if (tmp.equals(""))
					throw new WrongInputException("没有输入数据,请重新输入");
				t.setName(tmp);// 对读入的数据进行内部赋值

				System.out.println("请输入专业");
				tmp = br.readLine();// 数据的读入,以回车键作为输出结束的判读
				if (tmp.equals(""))// 判断是否进行了数据的输入,没有输入则报错

					throw new WrongInputException("没有输入数据,请重新输入");
				t.setMajor(tmp);

				System.out.println("请输入学号");
				tmp = br.readLine();
				if (tmp.equals(""))
					throw new WrongInputException("没有输入数据,请重新输入");
				t.setNum(tmp);

				System.out.println("请输入密码");
				tmp = br.readLine();
				if (tmp.equals(""))
					throw new WrongInputException("没有输入数据,请重新输入");
				t.setKeyword(tmp);
				System.out.println(t.getName());
				System.out.println(t.getMajor());
				System.out.println(t.getNum());
				System.out.println(t.getKeyword());

			} catch (IOException e) {
				System.out.println(e);
				e.printStackTrace();
			} catch (WrongInputException e) {
				System.out.println(e.getMessage());
				// System.out.println(e);
				// e.printStackTrace();

			} catch (Exception e) {
				// System.out.println("请输入数字,不要输入其他字符!");
				// System.out.println(e.getMessage());
				System.out.println(e);
				e.printStackTrace();
			}

		}
	}

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值