package cn.itcast_01;
import java.util.Scanner;
/*
* 校验qq号码.
* 1:要求必须是5-15位数字
* 2:0不能开头
*
* 分析:
* A:键盘录入一个QQ号码
* B:写一个功能实现校验
* C:调用功能,输出结果。
*/
public class RegexDemo {
public static void main(String[] args) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ号码:");
String qq = sc.nextLine();
System.out.println("checkQQ:"+checkQQ(qq));
}
/*
* 写一个功能实现校验 两个明确: 明确返回值类型:boolean 明确参数列表:String qq
*/
public static boolean checkQQ(String qq) {
boolean flag = true;
// 校验长度
if (qq.length() >= 5 && qq.length() <= 15) {
// 0不能开头
if (!qq.startsWith("0")) {
// 必须是数字
char[] chs = qq.toCharArray();
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
if (!Character.isDigit(ch)) {
flag = false;
break;
}
}
} else {
flag = false;
}
} else {
flag = false;
}
return flag;
}
}
package cn.itcast_01;
import java.util.Scanner;
/*
* 正则表达式:符合一定规则的字符串。
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ号码:");
String qq = sc.nextLine();
System.out.println("checkQQ:" + checkQQ(qq));
}
public static boolean checkQQ(String qq) {
// String regex ="[1-9][0-9]{4,14}";
// //public boolean matches(String regex)告知此字符串是否匹配给定的正则表达式
// boolean flag = qq.matches(regex);
// return flag;
//return qq.matches("[1-9][0-9]{4,14}");
return qq.matches("[1-9]\\d{4,14}");
}
}
1:正则表达式(理解)
(1)就是符合一定规则的字符串
(2)常见规则
A:字符
x 字符 x。举例:'a'表示字符a
\\ 反斜线字符。 代表一个反斜线。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? \.(写法\\.)
\d 数字:[0-9] 写法(\\d)
\w 单词字符:[a-zA-Z_0-9] 写法(\\w)
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界 写法(\\b)
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有 零次或者
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次 ,不能加空格
X{n,m} X,至少 n 次,但是不超过 m 次
(3)常见功能:(分别用的是谁呢?)
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex)
C:替换功能
String类的public String replaceAll(String regex,String replacement)
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
find():查找存不存在
group():获取刚才查找过的数据
package cn.itcast_05;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 获取功能
* Pattern和Matcher类的使用
*
* 模式和匹配器的基本使用顺序
*/
public class RegexDemo {
public static void main(String[] args) {
// 模式和匹配器的典型调用顺序
// 把正则表达式编译成模式对象
Pattern p = Pattern.compile("a*b");
// 通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串
Matcher m = p.matcher("aaaaab");
// 调用匹配器对象的功能
boolean b = m.matches();
System.out.println(b);
//这个是判断功能,但是如果做判断,这样做就有点麻烦了,我们直接用字符串的方法做
String s = "aaaaab";
String regex = "a*b";
boolean bb = s.matches(regex);
System.out.println(bb);
}
}
(4)案例
A:判断电话号码和邮箱
package cn.itcast_02;
import java.util.Scanner;
/*
* 判断功能
* String类的public boolean matches(String regex)
*
* 需求:
* 判断手机号码是否满足要求?
*
* 分析:
* A:键盘录入手机号码
* B:定义手机号码的规则
* 13436975980
* 13688886868
* 13866668888
* 13456789012
* 13123456789
* 18912345678
* 18886867878
* 18638833883
* C:调用功能,判断即可
* D:输出结果
*/
public class RegexDemo {
public static void main(String[] args) {
//键盘录入手机号码
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的手机号码:");
String phone = sc.nextLine();
//定义手机号码的规则
String regex = "1[38]\\d{9}";
//调用功能,判断即可
boolean flag = phone.matches(regex);
//输出结果
System.out.println("flag:"+flag);
}
}
package cn.itcast_02;
import java.util.Scanner;
/*
* 校验邮箱
*
* 分析:
* A:键盘录入邮箱
* B:定义邮箱的规则
* 1517806580@qq.com
* liuyi@163.com
* linqingxia@126.com
* fengqingyang@sina.com.cn
* fqy@itcast.cn
* C:调用功能,判断即可
* D:输出结果
*/
public class RegexTest {
public static void main(String[] args) {
//键盘录入邮箱
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱:");
String email = sc.nextLine();
//定义邮箱的规则
//String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
//调用功能,判断即可
boolean flag = email.matches(regex);
//输出结果
System.out.println("flag:"+flag);
}
}
B:按照不同的规则分割数据
package cn.itcast_03;
import java.util.Scanner;
/*
* 分割功能
* String类的public String[] split(String regex)
* 根据给定正则表达式的匹配拆分此字符串。
*
* 举例:
* 百合网,世纪佳缘,珍爱网,QQ
* 搜索好友
* 性别:女
* 范围:"18-24"
*
* age>=18 && age<=24
*/
public class RegexDemo {
public static void main(String[] args) {
//定义一个年龄搜索范围
String ages = "18-24";
//定义规则
String regex = "-";
//调用方法
String[] strArray = ages.split(regex);
// //遍历
// for(int x=0; x<strArray.length; x++){
// System.out.println(strArray[x]);
// }
//如何得到int类型的呢?
int startAge = Integer.parseInt(strArray[0]);
int endAge = Integer.parseInt(strArray[1]);
//键盘录入年龄
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if(age>=startAge && age<=endAge) {
System.out.println("你就是我想找的");
}else {
System.out.println("不符合我的要求,gun");
}
}
}
package cn.itcast_03;
/*
* 分割功能练习
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 定义一个字符串
String s1 = "aa,bb,cc";
// 直接分割
String[] str1Array = s1.split(",");
for (int x = 0; x < str1Array.length; x++) {
System.out.println(str1Array[x]);
}
System.out.println("---------------------");
String s2 = "aa.bb.cc";
String[] str2Array = s2.split("\\.");
for (int x = 0; x < str2Array.length; x++) {
System.out.println(str2Array[x]);
}
System.out.println("---------------------");
String s3 = "aa bb cc";
String[] str3Array = s3.split(" +");
for (int x = 0; x < str3Array.length; x++) {
System.out.println(str3Array[x]);
}
System.out.println("---------------------");
//硬盘上的路径,我们应该用\\替代\
String s4 = "E:\\JavaSE\\day14\\avi";
String[] str4Array = s4.split("\\\\");
for (int x = 0; x < str4Array.length; x++) {
System.out.println(str4Array[x]);
}
System.out.println("---------------------");
}
}
package cn.itcast_03;
import java.util.Arrays;
/*
* 我有如下一个字符串:"91 27 46 38 50"
* 请写代码实现最终输出结果是:"27 38 46 50 91"
*
* 分析:
* A:定义一个字符串
* B:把字符串进行分割,得到一个字符串数组
* C:把字符串数组变换成int数组
* D:对int数组排序
* E:把排序后的int数组在组装成一个字符串
* F:输出字符串
*/
public class RegexTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "91 27 46 38 50";
// 把字符串进行分割,得到一个字符串数组
String[] strArray = s.split(" ");
// 把字符串数组变换成int数组
int[] arr = new int[strArray.length];
for (int x = 0; x < arr.length; x++) {
arr[x] = Integer.parseInt(strArray[x]);
}
// 对int数组排序
Arrays.sort(arr);
// 把排序后的int数组在组装成一个字符串
StringBuilder sb = new StringBuilder();
for (int x = 0; x < arr.length; x++) {
sb.append(arr[x]).append(" ");
}
//转化为字符串
String result = sb.toString().trim();
//输出字符串
System.out.println("result:"+result);
}
}
C:把论坛中的数字替换为*
package cn.itcast_04;
/*
* 替换功能
* String类的public String replaceAll(String regex,String replacement)
* 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
*/
public class RegexDemo {
public static void main(String[] args) {
// 定义一个字符串
String s = "helloqq12345worldkh622112345678java";
// 我要去除所有的数字,用*给替换掉
// String regex = "\\d+";
// String regex = "\\d";
//String ss = "*";
// 直接把数字干掉
String regex = "\\d+";
String ss = "";
String result = s.replaceAll(regex, ss);
System.out.println(result);
}
}
D:获取字符串中由3个字符组成的单词
package cn.itcast_05;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 获取功能:
* 获取下面这个字符串中由三个字符组成的单词
* da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 定义字符串
String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
// 规则
String regex = "\\b\\w{3}\\b";
// 把规则编译成模式对象
Pattern p = Pattern.compile(regex);
// 通过模式对象得到匹配器对象
Matcher m = p.matcher(s);
// 调用匹配器对象的功能
// 通过find方法就是查找有没有满足条件的子串
// public boolean find()
// boolean flag = m.find();
// System.out.println(flag);
// // 如何得到值呢?
// // public String group()
// String ss = m.group();
// System.out.println(ss);
//
// // 再来一次
// flag = m.find();
// System.out.println(flag);
// ss = m.group();
// System.out.println(ss);
while (m.find()) {
System.out.println(m.group());
}
// 注意:一定要先find(),然后才能group()
// IllegalStateException: No match found
// String ss = m.group();
// System.out.println(ss);
}
}
2:Math(掌握)
(1)针对数学运算进行操作的类
(2)常见方法(自己补齐)
A:绝对值
B:向上取整
C:向下取整
D:两个数据中的大值
E:a的b次幂
F:随机数
G:四舍五入
H:正平方根
package cn.itcast_01;
/*
* Math:用于数学运算的类。
* 成员变量:
* public static final double PI
* public static final double E
* 成员方法:
* public static int abs(int a):绝对值
* public static double ceil(double a):向上取整,获取的是大于或者等于a的最小值
* public static double floor(double a):向下取整,获取的是小于或者等于a的最大值
* public static int max(int a,int b):最大值
* public static int min(int a,int b):最小值
* public static double pow(double a,double b):a的b次幂
* public static double random():随机数 [0.0,1.0)
* public static int round(float a) 四舍五入(参数为double的自学)
* public static double sqrt(double a):正平方根
*/
public class MathDemo {
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);
// public static final double E
System.out.println("E:" + Math.E);
System.out.println("--------------");
// public static int abs(int a):绝对值
System.out.println("abs:" + Math.abs(10));
System.out.println("abs:" + Math.abs(-10));
System.out.println("--------------");
// public static double ceil(double a):向上取整
System.out.println("ceil:" + Math.ceil(12.34));
System.out.println("ceil:" + Math.ceil(12.56));
System.out.println("--------------");
// public static double floor(double a):向下取整
System.out.println("floor:" + Math.floor(12.34));
System.out.println("floor:" + Math.floor(12.56));
System.out.println("--------------");
// public static int max(int a,int b):最大值
System.out.println("max:" + Math.max(12, 23));
// 需求:我要获取三个数据中的最大值
// 方法的嵌套调用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要获取四个数据中的最大值
System.out.println("max:"
+ Math.max(Math.max(12, 78), Math.max(34, 56)));
System.out.println("--------------");
// public static double pow(double a,double b):a的b次幂
System.out.println("pow:" + Math.pow(2, 3));
System.out.println("--------------");
// public static double random():随机数 [0.0,1.0)
System.out.println("random:" + Math.random());
// 获取一个1-100之间的随机数
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------");
// public static int round(float a) 四舍五入(参数为double的自学)
System.out.println("round:" + Math.round(12.34f));
System.out.println("round:" + Math.round(12.56f));
System.out.println("--------------");
//public static double sqrt(double a):正平方根
System.out.println("sqrt:"+Math.sqrt(4));
}
}
(3)案例:
A:猜数字小游戏
B:获取任意范围的随机数
B:
package cn.itcast_02;
import java.util.Scanner;
/*
* 需求:请设计一个方法,可以实现获取任意范围内的随机数。
*
* 分析:
* A:键盘录入两个数据。
* int strat;
* int end;
* B:想办法获取在start到end之间的随机数
* 我写一个功能实现这个效果,得到一个随机数。(int)
* C:输出这个随机数
*/
public class MathDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入开始数:");
int start = sc.nextInt();
System.out.println("请输入结束数:");
int end = sc.nextInt();
for (int x = 0; x < 100; x++) {
// 调用功能
int num = getRandom(start, end);
// 输出结果
System.out.println(num);
}
}
/*
* 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end
*/
public static int getRandom(int start, int end) {
// 回想我们讲过的1-100之间的随机数
// int number = (int) (Math.random() * 100) + 1;
// int number = (int) (Math.random() * end) + start;
// 发现有问题了,怎么办呢?
int number = (int) (Math.random() * (end - start + 1)) + start;
return number;
}
}
3:Random(理解)
(1)用于产生随机数的类
(2)构造方法:
A:Random() 默认种子,每次产生的随机数不同
B:Random(long seed) 指定种子,每次种子相同,随机数就相同
(3)成员方法:
A:int nextInt() 返回int范围内的随机数
B:int nextInt(int n) 返回[0,n)范围内的随机数
package cn.itcast_01;
import java.util.Random;
/*
* Random:产生随机数的类
*
* 构造方法:
* public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
* public Random(long seed):给出指定的种子 这个构造方法,每一次生成的随机数都是相同(用处小)
*
* 给定种子后,每次得到的随机数是相同的。
*
* 成员方法:
* public int nextInt():返回的是int范围内的随机数
* public int nextInt(int n):返回的是[0,n)范围的内随机数
*/
public class RandomDemo {
public static void main(String[] args) {
// 创建对象
// Random r = new Random();
Random r = new Random(1111);
for (int x = 0; x < 10; x++) {
// int num = r.nextInt();
int num = r.nextInt(100) + 1;
System.out.println(num); `
}
}
}
4:System(掌握)
(1)系统类,提供了一些有用的字段和方法
package cn.itcast_01;
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
protected void finalize() throws Throwable {
System.out.println("当前的对象被回收了" + this);
super.finalize();
}
}
(2)成员方法(自己补齐)
A:运行垃圾回收器
package cn.itcast_01;
/*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status)
* public static long currentTimeMillis()
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
*/
public class SystemDemo {
public static void main(String[] args) {
Person p = new Person("赵雅芝", 60);
System.out.println(p);
p = null; // 让p不再指定堆内存
System.gc();
}
}
B:退出jvm
C:获取当前时间的毫秒值
package cn.itcast_02;
/*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
* public static long currentTimeMillis():返回以毫秒为单位的当前时间
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
*/
public class SystemDemo {
public static void main(String[] args) {
// System.out.println("我们喜欢林青霞(东方不败)");
// System.exit(0);
// System.out.println("我们也喜欢赵雅芝(白娘子)");
// System.out.println(System.currentTimeMillis());
// 单独得到这样的实际目前对我们来说意义不大
// 那么,它到底有什么作用呢?
// 要求:请大家给我统计这段程序的运行时间
long start = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
System.out.println("hello" + x);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
}
D:数组复制
package cn.itcast_03;
import java.util.Arrays;
/*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
* public static long currentTimeMillis():返回以毫秒为单位的当前时间
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
* 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
*/
public class SystemDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 6, 7, 8, 9, 10 };
// 请大家看这个代码的意思
System.arraycopy(arr, 1, arr2, 2, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}