正则表达式
基础演示
public class Test {
public static void main(String[] args) {
System.out.println(check("7029622989")); // true
}
public static boolean check(String userId) {
return userId != null && userId.matches("[1-9]\\d{5,19}"); // 非0数字开头,全部是数字,长度是6~20
}
}
爬虫小案例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String data = "我们都是追梦人\n" +
"小明的电话是15697534597"+
"12545798632是小花的电话"+
"公司的邮箱地址123@qq.com"+
"今天天气很好啊,666"+
"还行吧,88啦";
// 1. 定义表达式的规则
String regex = "[1-9]\\d{10}";
// 2. 把字符串封装成一个 Pattern 对象
Pattern pattern = Pattern.compile(regex);
// 3. 通过 Pattern 对象去获取查找内容的匹配器对象
Matcher matcher = pattern.matcher(data);
// 4. 通过循环,爬取信息
while (matcher.find()){
String res = matcher.group();
System.out.println(res);
}
}
}
异常
认识异常
Error:代表系统级别错误,Error 不是给我们程序员用的,通常开发人员不必理会
Exception:异常
- 运行时异常:RuntimeException及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如:数组索引越界)
- 编译时异常:编译阶段就会出现错误提醒的(如:日期解析异常)
处理异常的两种方法:
- 抛出异常:throws,在方法上使用 throws 关键字,可以将方法内部出现的异常抛出去给调用者处理
- 捕获异常:try…catch,直接捕获程序出现的异常
自定义异常
自定义"运行时异常":只有在程序运行时才会提示异常
public class Test {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge(999);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("over");
}
public static void saveAge(int age) {
if (age > 0 && age < 150) {
System.out.println("Successful");
} else {
// throw 抛这个异常对象出去
throw new AgeIllegalRuntimeException("Age is illegal, Your age is '" + age + "'");
}
}
}
// 注意!一定要继承 RuntimeException 类,才能成为一个自定义的运行时异常类
class AgeIllegalRuntimeException extends RuntimeException {
public AgeIllegalRuntimeException() {
}
// 重写构造器之————封装异常的原因
public AgeIllegalRuntimeException(String message) {
super(message);
}
}
自定义"编译时异常":写代码的过程中就提示异常
public class Test {
public static void main(String[] args) {
// 需求:也是保存一个合法的年龄
try {
saveAge(666);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("over");
}
public static void saveAge(int age) throws AgeIllegalException {
if (age > 0 && age < 150) {
System.out.println("Successful");
} else {
// throw 抛这个异常对象出去
// throws 用在方法上,抛出方法内部的异常
throw new AgeIllegalException("Age is illegal, Your age is '" + age + "'");
}
}
}
// 注意!一定要继承 Exception 类,才能成为一个自定义的编译时异常类
class AgeIllegalException extends Exception {
public AgeIllegalException() {
}
public AgeIllegalException(String message) {
super(message);
}
}
异常处理
捕获异常,记录异常并响应合适的信息给用户
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
try {
run1();
} catch (Exception e) {
System.out.println("错误了哦~~~"); // 显示给用户看
e.printStackTrace(); // 记录异常,显示给管理员看
}
}
public static void run1() throws Exception {
SimpleDateFormat obj = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date data = obj.parse("2023-11-25 12:25");
run2();
}
public static void run2() throws Exception {
// 读取文件
InputStream is = new FileInputStream("E:/img.png");
}
}
捕获异常,尝试重新修复
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 尝试修复
while (true) {
try {
System.out.println(getMoney());
break;
} catch (Exception e) {
System.out.println("请您输入合法的数字!");
}
}
}
public static double getMoney() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入合适的金额:");
double money = sc.nextDouble();
if (money >= 0) {
return money;
} else {
System.out.println("你输入的金额对方不满意!");
}
}
}
}