1.Scanner概述:JDK5以后用于获取用户的键盘输入
Scanner 一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器
Scanner的构造方法原理
Scanner(InputStream source) 构造一个新的Scanner 它生成的值是从指定的输入流扫描的
System类下有一个静态的字段:
public static final InputStream in; 标准的输入流,对应着键盘录入。
案例分析:
public class ScannerDemo {
public static void main(String[] args) {
InputStream in = System.in;//in 标准输入流 此流已打开并准备提供输入数据 通常,此流对应于键盘输入
Scanner sc = new Scanner(in);
int i = sc.nextInt();
double v = sc.nextDouble();
long l = sc.nextLong();
//录入布尔类型
boolean b = sc.nextBoolean();
//录入字符串
String s = sc.nextLine();
}
}
2.Scanner类的hasNextXxx()和nextXxx()方法
hasNextXxx() 判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
如果需要判断是否包含下一个字符串,则可以省略Xxx
nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
案例分析:
public class ScannerDemo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请录入整数");
//判断输入的数据类型是不是int类型
// boolean b = scanner.hasNextInt();
if(sc.hasNextInt()){
int num = sc.nextInt();
System.out.println(num);
}else{
System.out.println("你输入的数据不正确,请重新输入一个数据");
}
}
}
3. public int nextInt():获取一个int类型的值
public String nextLine():获取一个String类型的值
public String next():获取一个String类型的值
案例:public class ScannerDemo2 {
public static void main(String[] args) {
/*Scanner sc = new Scanner(System.in);
System.out.println("请录入一个整数");
int num = sc.nextInt();
System.out.println(num);
System.out.println("请录入一个字符串");
String s = sc.nextLine();
System.out.println(s);*/
//当我们先录入一个整数,在录入一个字符串时,发现字符串并没有录入进去。
//因为你输入完数字后,敲了回车,那么这个回车换行其实被当做字符 然后 nextLine() 这个方法,把回车换行录入进去了。这种现象不是你想要的。那怎么解决。
//解决方式1. 你可以录完整数,在录入字符串之前,重新再创建一个Scanner对象。
/* Scanner sc = new Scanner(System.in);
System.out.println("请录入一个整数");
int num = sc.nextInt();
System.out.println(num);
//重新创建一个新的对象
sc=new Scanner(System.in);
System.out.println("请录入一个字符串");
String s = sc.nextLine();
System.out.println(s);*/
System.out.println("======================================");
//解决方法2:可以在录入字符串时,换一个next()方法
Scanner sc = new Scanner(System.in);
System.out.println("请录入一个整数");
int num = sc.nextInt();
System.out.println(num);
System.out.println("请录入一个字符串");
//换成这个next()也能录入字符串
String s = sc.next(); //这个方法不会录入回车换行。
System.out.println(s);
}
}
String类
1.String类:
字符串:字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
a:字符串字面值"abc"也可以看成是一个字符串对象。
b:字符串是常量,一旦被创建,就不能被改变。
2.String类的构造方法:
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
案例1分析:
public class MyTest{
public static void main(String[] args) {
//"" 空串
/*System.out.println("");
String s = new String();
System.out.println(s.toString());
String s1 = new String("abc");
System.out.println(s1.toString());*/
// public String( byte[] bytes):把字节数组转成字符串
//定义一个字节数组
byte[] bytes={97,98,99,100};
String s = new String(bytes);
System.out.println(s);//"abcd"
System.out.println("=======================");
把一个字节数组的一部分转换成字符串
// //参数2 字节数组中元素的索引。参数3 你要转几个字节
String s1 = new String(bytes, 1, 2);
System.out.println(s1.toString());
}
}
案例2:
public class MyTest2 {
public static void main(String[] args) {
//把字符数组转换成字符串
char[] chars={'a','b','c','我','爱','你'};
/*//自己实现
String s = new String();
for (int i = 0; i < chars.length; i++) {
s+=chars[i];
}
System.out.println(s.toString());*/
//我们可以通过构造方法来把字符数组转换成字符串
/* String( char[] value)
分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。*/
String s = new String(chars);
System.out.println(s);
System.out.println("=========================");
//我们可以把字符数组的一部分转换成字符串
/* String( char[] value, int offset, int count)
分配一个新的 String,它包含取自字符数组参数一个子数组的字符。*/
//从字符数组的4索引所对应的元素开始,转换3个字符为字符串
String s1 = new String(chars, 3, 3);
System.out.println(s1.toString());
}
}
3.String的特点:一旦被创建就不能改变
如何理解这句话
String s = "hello" ;
s = "world" + "java"; 问s的结果是多少?
s的结果是worldjava
这里指的是,s是堆内存中的字符串常量值,这个值是常量,不能被改变
但是s所指向的引用可以被改变
4.String s = new String(“hello”)和String s = “hello”;的区别
String s = new String(“hello”)这句代码是指创建了两个对象
首先在堆内存的字符串常量池中创建字符串常量“hello"
随后创建s1对象,s1对象中存放的是字符串常量池中"hello"的地址
String s = “hello”这句代码是创建了一个对象
5.String类的判断功能
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""
案例分析:
public class MyTest2 {
public static void main(String[] args) {
//判断两个字符串,字面上的内容是否相同,区分大小写
boolean b = "abc".equals("ABC");
System.out.println(b);
//判断两个字符串,字面上的内容是否相同,不区分大小写
boolean b1 = "abc".equalsIgnoreCase("ABC");
System.out.println(b1);
//判断一个字符串是否是空串
boolean b2 = "".isEmpty();
System.out.println(b2);
if("".length()==0){
System.out.println("是空串");
}
//判断一个字符串是不是以这个开头
boolean b3 = "张若昀".startsWith("张");
System.out.println(b3);
//判断一个字符串是不是以这个结尾
boolean b4 = "杨洋".endsWith("洋");
System.out.println(b4);
//判断一个字符串,是否包含一个子串
boolean b5 = "我爱你".contains("我");
System.out.println(b5);
}
}
案例演示: 需求:模拟登录,给三次机会,并提示还有几次。
public class MyTest3 {
public static void main(String[] args) {
/*A:
案例演示:
需求:模拟登录, 给三次机会, 并提示还有几次。*/
//用户名和密码,已经从数据库查出来
String username = "张三";
String password = "123456";
//获取用户从键盘输入的用户名和密码,来模拟登陆
Scanner sc = new Scanner(System.in);
for(int i=1;i<=3;i++){
System.out.println("请输入用户名");
String uname = sc.nextLine();
System.out.println("请输入密码");
String pwd = sc.nextLine();
//把用户输入的数据和我们查出来的数据进行比对
if(username.equals(uname)&&password.equals(pwd)){
System.out.println("登陆成功");
break;
}else{
if((3-i)==0){
System.out.println("登录失败,机会已经用完");
}else{
System.out.println("登录失败,用户名或密码输入错误,请重新输入,你还有"+(3-i)+"次机会");
}
}
}
}
}
6.String类的获取功能
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
案例分析:
public class MyTest {
public static void main(String[] args) {
// public int length ():获取字符串的长度。
//根据索引获取单个字符
String s = "abcdefbc我爱你我爱你";
//charAt(索引) 根据索引,来获取这个字符串中的某个字符
char ch = s.charAt(s.length() - 1);
System.out.println(ch);
//查找该字符或者字符串第一次出现的索引
int index = s.indexOf("我");
System.out.println(index);
//indexOf 从左往右 从头开始找
int index1 = s.indexOf("bc");
System.out.println(index1);
//从指定索引处往后查找
int index2 = s.indexOf("bc", 2);
System.out.println(index2);
int index3 = s.indexOf("bc", s.indexOf("bc") + 1);
System.out.println(index3);
//调用indexOf() 如果没有找到 返回 -1 -1 经常用来代表没找到 我们经常用 -1来作为判断条件
int index4 = s.indexOf("呵呵");
System.out.println(index4);
System.out.println("====================");
//从后往前找,该字符或字符串第一次出现的索引
String s2="像我这样的人,本该灿烂过一生我";
int index5 = s2.lastIndexOf("我", 10);
System.out.println(index5);
//怎么判断一个字符在字符串中只出现过一次
//怎么判断这个坏字 在这个字符串中只出现过一次。
String s3 = "像我这样的人本该灿烂过一生坏我像我这样的人本该灿烂过一生我像我这样的人,本该灿烂过一生我";
int i = s3.indexOf("坏");
int j = s3.lastIndexOf("坏");
//从头开始找到第一个坏字的下标如果和从尾找到的第一个坏字的下标相同
//那么这个字符串中就只有这一个坏字
System.out.println(i==j);
System.out.println("==================================");
//截取字符串
String s4 ="我在人民广场吃着炸鸡而此时此刻你在哪里";
//从指定索引处,截取到末尾返回
String s1 = s4.substring(5);
System.out.println(s1);
//如果要截取某一段 可以指定两个下标
String ss = s4.substring(0, 6);//含头不含尾
System.out.println(ss);
}
}
7.字符串的遍历
public class MyTest2 {
public static void main(String[] args) {
String s4 = "我在人民广场吃着炸鸡而此时此刻你在哪里";
//遍历字符串
//顺序遍历
for (int i = 0; i < s4.length(); i++) {
char ch = s4.charAt(i);
System.out.println(ch);
}
System.out.println("=====================");
//倒序遍历
for (int j = s4.length()-1; j >= 0; j--) {
char ch = s4.charAt(j);
System.out.println(ch);
}
}
}
8.案例演示: 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。
案例:
public class MyTest3 {
public static void main(String[] args) {
/* A:
案例演示:
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/
String str="asdfaASFfdcfdf144149855001asdfasAFdcdf";
//遍历,遍历途中,就得判断是什么字符,然后统计
int daxie=0;
int xiaoxie=0;
int num=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>=65 &&str.charAt(i)<=91){
daxie++;
}
if(str.charAt(i)>=97 &&str.charAt(i)<=123){
xiaoxie++;
}
if(str.charAt(i)>=48 &&str.charAt(i)<=58){
num++;
}
}
System.out.println("大写字母个数"+daxie);
System.out.println("小写字母个数"+xiaoxie);
System.out.println("数字个数"+num);
System.out.println(str.length());
}
}
9.String类的转换功能
public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
案例1:
public class MyTest1 {
public static void main(String[] args) {
//把一个字符串转换成字节数组
String str="abcd";
byte[] bytes = str.getBytes();
System.out.println(bytes.length);
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]+" ");
}
System.out.println();
System.out.println("============================");
//把字节数组转换成字符串
String s = new String(bytes);
System.out.println(s);
System.out.println("==============================");
String string="我爱你";
//UTF-8编码中一个汉字占三个字节
byte[] bytes1 = string.getBytes();
System.out.println(bytes1.length);
System.out.println("======================");
for (int i = 0; i < bytes1.length; i++) {
System.out.println(bytes1[i]);
}
System.out.println("===================");
String s1 = new String(bytes1, 0, 6);
System.out.println(s1);
}
}
案例2:
public class MyTest3 {
public static void main(String[] args) {
//valueOf()方法,可以把很多类型转换成字符串
//把一个整数转换成字符串
int num=100;
String str=num+"";//拼接空串
System.out.println(str);
String s = String.valueOf(num);//"100"
String a = String.valueOf('a');
String s1 = String.valueOf(3.14);
String s2 = String.valueOf(false);
//把一个字符数组转换成字符串
String s3 = String.valueOf(new char[]{'a', 'b', 'c'});//"abc"
String s4 = new String(new char[]{'a', 'b', 'c'});//"abc"
//转换成大写
String s5 = "abcd".toUpperCase();
System.out.println(s5);
//转换成小写
String s6 = "ABCD".toLowerCase();
System.out.println(s6);
//拼接字符串
String ss="aaa";
String s7="bbb";
String s8=ss+s7;
String s9="aa"+"bb"+"cc";
System.out.println("======================");
//使用方法来拼接字符串
String sss = "aa".concat("bb");
System.out.println(sss);
System.out.println("=======================");
//链式编程
String concat = "aa".concat("bb").concat("cc").concat("dd").concat("ee");
System.out.println(concat);
}
}
案例演示: 需求:把一个字符串的首字母转成大写,其余为小写
public class MyTest4 {
public static void main(String[] args) {
// /A:
// 案例演示:
// 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
String str = "aBDEfdafaaafeDeFDf";
String index = str.substring(0, 1);
String s = index.toUpperCase();
String substring = str.substring(1);
substring=substring.toLowerCase();
String concat = s.concat(substring);
System.out.println(concat);
System.out.println("===========================");
//链式编程
String s1 = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
System.out.println(s1);
}
}
10.String类的其他功能
String的替换功能及案例演示
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
String的去除字符串两空格及案例演示
public String trim() 去除两端空格
str.replace(" ",""); 去除所有空格
String的按字典顺序比较两个字符串及案例演示
public int compareTo(String str)
会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
案例1:
public class MyTest {
public static void main(String[] args) {
String str="奥巴马和特朗普是美国总统";
//一次替换一个字符
String s = str.replace('奥', '*');
System.out.println(s);
//一次替换一个字符串
String str2= "奥巴马和特朗普是美国总统";
String s1 = str2.replace("奥巴马", "*").replace("特朗普", "*");
System.out.println(s1);
}
}
案例2:
分别去除两端空格,左端空格,右端空格,中间空格
public class MyTest {
public static void main(String[] args) {
String username=" zha ng san ";
//去除字符串两端空格
String trim = username.trim();
System.out.println("====="+trim+"=====");
System.out.println("----------------------------");
//作业1:去除掉字符串的左端空格
String username2 = " zha ng san ";
for (int i = 0; i < username2.length(); i++) {
char ch = username2.charAt(i);
if(ch!=' '){
String str1 = username2.substring(i);
System.out.println("====="+str1+"=====");
break;
}
}
//作业2:去掉字符串的右端空格
String username3 = " zha ng san ";
int index = username3.lastIndexOf('n');
String substring = username3.substring(0, index);
System.out.println("====="+substring+"=====");
//作业3:去掉字符串的中间空格
String username4 = " zha ng san ";
int index1 = username4.indexOf('z');
int index2 = username4.lastIndexOf('n');
String substring1 = username4.substring(index1, index2);
String replace = substring1.replace(" ", "");
String sss = username4.replace(substring1, replace);
System.out.println("==="+sss+"===");
}
}
11.案例分析:
需求:统计大串中小串出现的次数
举例: "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次
案例分析:
public class MyTest6 {
public static void main(String[] args) {
//6、请编写程序,统计键盘录入的字符串中出现了几次字符串”java”,并测试
//例: 键盘输入:woyaoxuejava,xihuanjava,aijava,javajavawozuiai
String str="woyaoxuejava,xihuanjava,aijava,javajavawozuiai";
String str1="java";
int count=0;
int index=0;
while((index=str.indexOf(str1))!=-1){
count++;
str = str.substring(index + str1.length());
}
System.out.println(count);
}
}