Day12
常见对象Scanner
Scanner的概述和方法
A:Scanner的概述
B:Scannner的构造方法原理
*Scanner(InputStream source)
*System类下有一个静态的字段;
*public static final InputStream in;标准的输入流,对着键盘录入;
C:一般方法
*hasnextXxx()判断是否还有下一个输入项,其中Xxx可以是Int,Double等,如果需要判断是否还有下一个字符串,可以省略Xxx;
*nextXxx(),获取下一个输入项,Xxx的含义和上个方法中的Xxx相同,默认情况下,Scanner使用空格,回车等作为分隔符
Scanner获取数据出现的小问题及解决方案
A:两个常用的方法
*public int nextInt():获取一个Int类型的值;
*public String nextLine():获取一个String类型的值;
B:案例演示
*a:先演示获取多个int值,多个String值的情况;
*b:再演示先获取int值,然后获取String值出现问题;
*解决方案:
*第一种:先获取一个数值后,在创建一个新的键盘录入对象获取字符串;
*第二种:把所有的数据都按照字符串获取,要什么,用的时候就强制转换成什么
package com.ning.scanner;
import java.util.Scanner;
public class Demo2_Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*
* System.out.println("请输入第一个整数"); int i = sc.nextInt();
* System.out.println("请输入第二个整数"); int j = sc.nextInt(); System.out.println("i="
* + i); System.out.println("j=" + j);
*/
/*
* System.out.println("请输入第一个字符串"); String line1 = sc.nextLine();
* System.out.println("请输入第二个字符串"); String line2 = sc.nextLine();
* System.out.println("line1 = " + line1 + ",line2 = asd" + line2);
*/
/*
* nextInt()是键盘录入整数的方法,当我们录入10的时候,其实在键盘上录入是10和\r\n,nextInt()只获取10就结束了
* nextLine是键盘录入字符串的方法,可以接受任意类型,但是他凭什么能获取一行呢? 通过\r\n,只要遇到\r\n就证明这一行结束
*/
/*
* System.out.println("请输入第一个整数"); int i = sc.nextInt();
* System.out.println("请输入第一个字符串"); String line1 = sc.nextLine();
* System.out.println("i = " + i + ",line1 = " + line1); System.out.println(i);
* System.out.println("-----------"); System.out.println(line1);
* System.out.println("222222222222");
*/
/*
* 解决方案
* 1.创建两次对象, 但是浪费空间;
* 2.将键盘录入的都是字符串,都用nextLine()方法
*/
int i = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
String line = sc2.nextLine();
System.out.println(i);
System.out.println(line);
}
}
String类
String类概述
A:String类的概述
*通过JDK提供的API,查看String类的说明
*可以看到这样两句话
:a:字符串字面值"abc"也可以看成是一个字符串对象;
:b:字符串是常量,一旦被赋值,不能被改变;
package com.ning.string;
public class Demo1_String {
/*
* a:字符串字面值"abc"也可以看成是一个字符串对象; :b:字符串是常量,一旦被赋值,不能被改变;
*/
public static void main(String[] args) {
String str = "abc"; // abc可以看成一个字符串对象
str = "def"; // 当把def赋值给str,原来的abc就变成了垃圾
System.out.println(str);// String类重写了toString方法
}
}
String类的构造方法
/*
* public String() :空构造;
* public String(byte[] bytes) :把字节组成字符串;
* public String(byte[] bytes,int index,int length):把字节数组的一部分组成字符串;
* public String(char[] value) :把字符组成字符串;
* public String(char[] value,int index,int
* count):把字符数组的一部分组成字符串;
* public String(String orignal):把字符串常量值转成字符串
*/
package com.ning.string;
public class Demo2_StringCon {
/*
* public String() :空构造;
* public String(byte[] bytes) :把字节组成字符串;
* public String(byte[] bytes,int index,int length):把字节数组的一部分组成字符串;
* public String(char[] value) :把字符组成字符串;
* public String(char[] value,int index,int
* count):把字符数组的一部分组成字符串;
* public String(String orignal):把字符串常量值转成字符串
*/
public static void main(String[] args) {
String s1 = new String();
System.out.println(s1);
byte[] arr1 = { 99, 98, 97 };
String s2 = new String(arr1); //解码,就是将计算机读的懂转换成我们读的懂的;
System.out.println(s2);
byte[] arr2 = {97,98,99,100,101,102};
String s3 = new String(arr2,2,3);
System.out.println(s3);
char[] data = {'q','w','e','r','t'};
String s4 = new String(data);
System.out.println(s4);
String s5 = new String(data,0,3);
System.out.println(s5);
String s6 = new String("ning");
System.out.println(s6);
}
}
String面试题
package com.ning.string;
public class Demo3_String {
public static void main(String[] args) {
demo1();
demo2();
demo3();
demo4();
demo5();
}
private static void demo5() {
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2); // false
System.out.println(s3.equals(s2));// true
}
private static void demo4() {
// byte b = 3 + 4; //在编译时就编译成7,把7赋值给b,常量优化机制
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2); // true ,java中有常量优化机制
System.out.println(s1.equals(s2));// true
}
private static void demo3() {
String s1 = new String("abc"); // 记录堆内存地址值;
String s2 = "abc";// 记录常量池地址值;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
private static void demo2() {
// 创建几个对象?
String s1 = new String("abc");
}
private static void demo1() { // 常量池中没有这个字符串就创建一个,如果有就直接用
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
String类的判断功能
/*
* boolean equals(Object obj):比较字符的内容是否相同,区分大小写;
* boolean equalsIgnoreCase(String str);比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endWith(String str):判断字符串是否以某个指定的字符串结尾;
* boolean isEmpty() :判断字符串是否为空
* ""空串是字符串常量,同时也是一个String类的对象;既然是一个对象,便可以调用方法
* null是空常量,不能调用任何方法,否则就会出现空指针异常,null可以给任意的引用值常量赋值
*/
package com.ning.string;
public class Demo4_StringMethod {
public static void main(String[] args) {
// demo1();
// demo2();
String s1 = "ning";
String s2 = "";
String s3 = null;
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
System.out.println(s3.isEmpty());
}
private static void demo2() {
String s1 = "我爱你,啦啦啦";
String s2 = "你";
String s3 = "他";
String s4 = "我";
String s5 = "爱";
System.out.println(s1.contains(s2));
System.out.println(s1.contains(s3));
System.out.println("--------");
System.out.println(s1.startsWith(s4));
System.out.println(s1.startsWith(s5));
System.out.println(s1.endsWith(s5));
}
private static void demo1() {
String s1 = "ning";
String s2 = "Ning";
String s3 = "ning";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println("-----------");
System.out.println(s1.equalsIgnoreCase(s2));
}
}
案例演示
package com.ning.test;
import java.util.Scanner;
public class Test1 {
/*
* A:案例演示
* 需求:模拟用户登录,给三次机会,并提示还有几次
* 用户名和密码都是admin
* 分析:
* 1.模拟登录,需要键盘录入,Scanner
* 2.给三次机会,需要循环,用for
* 3.并提示有几次,用判断if
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String userName = sc.nextLine(); // 将键盘输入内容存储在userName中
System.out.println("请输入密码");
String passWord = sc.nextLine(); // 将键盘输入内容存储在userName中
// 如果是字符串常量和字符串变量进行比较,一般都是字符串常量调用方法,将变量当做参数传递,防止空指针异常
if ("admin".equals(userName) && "admin".equals(passWord)) {
System.out.println("欢迎" + userName + "登录");
break;
} else {
if (i == 2) {
System.out.println("您的错误次数已到,请明天再来");
} else {
System.out.println("录入错误,您还有" + (2 - i) + "次机会");
}
}
}
}
}
String类的获取功能
A:String类的获取功能
*int length():获取字符串长度
*char charAt(int index)获取指定位置的字符;
*int indexOf(int ch):返回指定字符在此字符串第一次出现处的索引
*int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
*int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
*int indexOf(String str,int fromIndex):返回执行字符串在此字符串中从指定位置后第一次出现处的索引
*lastIndexof
*String substring(int start):从指定位置开始截取字符串,默认到末尾
*String substring(int start,int end)从指定位置开始,从指定位置结束,截取字符串
package com.ning.string;
public class Demo5_StringMethod {
/**
* A:String类的获取功能
*int length():获取字符串长度
*char charAt(int index)获取指定位置的字符;
*int indexOf(int ch):返回指定字符在此字符串第一次出现处的索引
*int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
*int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
*int indexOf(String str,int fromIndex):返回执行字符串在此字符串中从指定位置后第一次出现处的索引
*lastIndexof
*String substring(int start):从指定位置开始截取字符串,默认到末尾
*String substring(int start,int end)从指定位置开始,从指定位置结束,截取字符串
*/
public static void main(String[] args) {
// demo1();
// demo2();
String s1 = "woaizhonghua";
int index1 = s1.indexOf('a', 3);
System.out.println(index1);
int index2 = s1.indexOf("on", 2);
int index3 = s1.lastIndexOf('a');
System.out.println(index2);
System.out.println(index3); // 从后向前找,第一个出现的处的索引
String s2 = s1.substring(2);
System.out.println(s2);
String s3 = s1.substring(2, 6);
System.out.println(s3);
}
private static void demo2() {
String s1 = "ningyanhui";
int index = s1.indexOf('y'); // 传递char类型的会自动提升
System.out.println(index);
int index2 = s1.indexOf('z');// 如果不存在就返回-1
System.out.println(index2);
int index3 = s1.indexOf("hui");
System.out.println(index3);
}
private static void demo1() {
String s1 = "ningyanhui";
System.out.println(s1.length()); // length是一个方法,获取的是每一个字符的个数
String s2 = "啦啦啦啦啦!!";
System.out.println(s2.length());
System.out.println(s1.charAt(5));
char c2 = s2.charAt(10);
System.out.println(c2); // StringIndexOutOfBoundsException
}
}
案例演示:
需求:遍历字符串
package com.ning.test;
public class Test2 {
public static void main(String[] args) {
String s = "woaizhonghua";
for (int i = 0; i < s.length(); i++) {// 通过for循环,获取到字符串中每个字符的索引
char c = s.charAt(i);
System.out.print(c + "\t");
// System.out.println(s.charAt(i));
}
}
}
A:案例演示
*需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数
*ABCDEabcde123456!@#$%^
package com.ning.test;
public class Test3 {
/*
* 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数
* ABCDEabcde123456!@#$%^
* 分析:字符串由字符组成,而字符的值都是有范围的,通过范围来判断是否包含该字符,
* 如果包含,就让计数器变量自增
*/
public static void main(String[] args) {
String s = "ABCDEabcde123456!@#$%^";
int big = 0;
int small = 0;
int num = 0;
int other = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'A' && c <= 'Z') { // 如果满足大写字母,就让big自增
big++;
} else if (c >= 'a' && c <= 'z') {
small++;
} else if (c >= '0' && c <= '9') {
num++;
} else
other++;
// 打印计数器结果
System.out.println("大写字母有" + big + "个");
System.out.println("小写字母有" + small + "个");
System.out.println("数字有" + num + "个");
System.out.println("其他字符有" + other + "个");
}
}
}
String类的转换功能
package com.heima.string;
import com.heima.bean.Person;
public class Demo6_StringMethod {
/**
* * byte[] getBytes():把字符串转换为字节数组。
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
* String toLowerCase():把字符串转成小写。(了解)
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。
*/
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
String s1 = "heiMA";
String s2 = "chengxuYUAN";
String s3 = s1.toLowerCase();
String s4 = s2.toUpperCase();
System.out.println(s3);
System.out.println(s4);
System.out.println(s3 + s4); //用+拼接字符串更强大,可以用字符串与任意类型相加
System.out.println(s3.concat(s4)); //concat方法调用的和传入的都必须是字符串
}
private static void demo3() {
char[] arr = {'a','b','c'};
String s = String.valueOf(arr); //底层是由String类的构造方法完成的
System.out.println(s);
String s2 = String.valueOf(100); //将100转换为字符串
System.out.println(s2 + 100);
Person p1 = new Person("张三", 23);
System.out.println(p1);
String s3 = String.valueOf(p1); //调用的是对象的toString方法
System.out.println(s3);
}
private static void demo2() {
String s = "heima";
char[] arr = s.toCharArray(); //将字符串转换为字符数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void demo1() {
String s1 = "abc";
byte[] arr = s1.getBytes();
for (int i = 0; i < arr.length; i++) {
//System.out.print(arr[i] + " ");
}
String s2 = "你好你好";
byte[] arr2 = s2.getBytes(); //通过gbk码表将字符串转换成字节数组
for (int i = 0; i < arr2.length; i++) { //编码:把我们看的懂转换为计算机看的懂得
//System.out.print(arr2[i] + " "); //gbk码表一个中文代表两个字节
} //gbk码表特点,中文的第一个字节肯定是负数
String s3 = "琲";
byte[] arr3 = s3.getBytes();
for (int i = 0; i < arr3.length; i++) {
System.out.print(arr3[i] + " ");
}
}
}
案例演示
package com.heima.test;
public class Test4 {
/**
* * A:案例演示
* 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
* 链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
*/
public static void main(String[] args) {
String s = "woaiHEImaniaima";
String s2 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s2);
}
}
案例演示:把数组转换成字符串
public class Test5 {
/**
* * A:案例演示
* 需求:把数组中的数据按照指定个格式拼接成一个字符串
* 举例:
* int[] arr = {1,2,3};
* 输出结果:
* "[1, 2, 3]"
*
分析:
1,需要定义一个字符串"["
2,遍历数组获取每一个元素
3,用字符串与数组中的元素进行拼接
*/
public static void main(String[] args) {
int[] arr = {1,2,3};
String s = "["; //定义一个字符串用来与数组中元素拼接
for (int i = 0; i < arr.length; i++) { //{1,2,3}
if(i == arr.length - 1) {
s = s + arr[i] + "]"; //[1, 2, 3]
}else {
s = s + arr[i] + ", "; //[1, 2,
}
}
System.out.println(s);
}
}
String类的其他功能
package com.heima.string;
public class Demo7_StringMethod {
/**
* * A:String的替换功能及案例演示
* String replace(char old,char new)
* String replace(String old,String new)
* B:String的去除字符串两空格及案例演示
* String trim()
* C:String的按字典顺序比较两个字符串及案例演示
* int compareTo(String str)(暂时不用掌握)
* int compareToIgnoreCase(String str)(了解)
*
*/
public static void main(String[] args) {
//demo1();
//demo2();
String s1 = "a";
String s2 = "aaaa";
int num = s1.compareTo(s2); //按照码表值比较
System.out.println(num);
String s3 = "黑";
String s4 = "马";
int num2 = s3.compareTo(s4);
System.out.println('黑' + 0); //查找的是unicode码表值
System.out.println('马' + 0);
System.out.println(num2);
String s5 = "heima";
String s6 = "HEIMA";
int num3 = s5.compareTo(s6);
System.out.println(num3);
int num4 = s5.compareToIgnoreCase(s6);
System.out.println(num4);
/*
* public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1); //将c1字符转换成大写
c2 = Character.toUpperCase(c2); //将c2字符转换成大写
if (c1 != c2) {
c1 = Character.toLowerCase(c1); //将c1字符转换成小写
c2 = Character.toLowerCase(c2); //将c2字符转换成小写
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
*/
}
private static void demo2() {
String s = " hei ma ";
String s2 = s.trim();
System.out.println(s2);
}
private static void demo1() {
String s = "heima";
String s2 = s.replace('i', 'o'); //用o替换i
System.out.println(s2);
String s3 = s.replace('z', 'o'); //z不存在,保留原字符不改变
System.out.println(s3);
String s4 = s.replace("ei", "ao");
System.out.println(s4);
}
}
案例演示:
package com.heima.test;
import java.util.Scanner;
public class Test6 {
/**
* * A:案例演示
* 需求:把字符串反转
* 举例:键盘录入"abc"
* 输出结果:"cba"
*分析:
*1,通过键盘录入获取字符串Scanner
*2,将字符串转换成字符数组
*3,倒着遍历字符数组,并再次拼接成字符串
*4,打印
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一个字符串:");
String line = sc.nextLine(); //将键盘录入的字符串存储在line中
char[] arr = line.toCharArray(); //将字符串转换为字符数组
String s = "";
for(int i = arr.length-1; i >= 0; i--) { //倒着遍历字符数组
s = s + arr[i]; //拼接成字符串
}
System.out.println(s);
}
}
在字符串大串中统计小串出现的次数
package com.heima.test;
public class Test7 {
/**
* * A:画图演示
* 需求:统计大串中小串出现的次数
* 这里的大串和小串可以自己根据情况给出
*
*/
public static void main(String[] args) {
//定义大串
String max = "woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma";
//定义小串
String min = "heima";
//定义计数器变量
int count = 0;
//定义索引
int index = 0;
//定义循环,判断小串是否在大串中出现
while((index = max.indexOf(min)) != -1) {
count++; //计数器自增
max = max.substring(index + min.length());
}
System.out.println(count);
}
}