常见对象中的问题2
目录
1.Scanner的使用
(1)在JDK5以后出现的用于键盘录入数据的类。
(2)构造方法:
A:讲解了System.in这个东西
它其实是标准的输入流,对应于键盘录入
B:构造方法
InputStream is = System.in;
Scanner(InputStream is)
C:常用格式:
Scanner sc = new Scanner(System.in);
(3)基本方法格式:
A:hasNextXxx()判断是否是某种类型的
B:nextXxx返回某种类型的元素
(4)要掌握的两个方法
A:public int nextInt()
B:public String nextLine()
(5)需要注意的小问题
A:同一个Scanner对象,先获取数值,在获取字符串会出现一个小问题
B:解决方案
a:重新定义一个Scanner对象
b:把所有的数据都用字符串获取,然后再进行相互的转换
2.String类的概述和使用
(1)多个字符组成的一串数据
其实它可以和字符数组进行相互转换
(2)构造方法
A:public String():空构造
B:public String(byte[] bytes):把字节数组转成字符串
C:public String(byte[] bytes,int offset,int length):把字节数组的一部分转成字符串
D:public String(char[] value):把字符数组转成字符串
E:public String(char[] value,int offset,int count):把字符数组的一部分转换成字符串
F:public String(String original)把字符串常量值转换成字符串
下面的这个虽然不是构造方法,但是结构也是一个字符串对象
G:String s =“helloworld”;
(3)字符串的特点:
A:字符串一旦被赋值,就不能改变
注意:这里指的是字符串的内容不能改变,而不是引用不能改变
B:字面值作为字符串对象和通过构造方法创建对象的不同
String s = new String(“hello”);和String s = "hello"的区别
(4)==和equals()字符串的拼接
(5)字符串的功能
A:判断功能
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空
B:获取功能
int length():获取字符串的长度
char charAt(int index):获取指定索引位置的字符
int indexof(int ch):返回指定字符在此字符串中第一次出现处的索引
为什么这里是int类型,而不是char类型?
原因是:‘a’和97其实都可以代表’a’
int indexof(String str):返回指定字符串在此字符串中第一次出现处的索引
int indexof(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
String substring(int start):从指定位置开始截取字符串,默认到结尾
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
C:转换功能
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):把字符串拼接
D:其他功能
a:替换功能:replace:替换
String replace(char old,char new)
String replace(String old,String new)
b:去除字符串两空格
String trim()
c:按字典顺序比较两个字符:
int compareTo(String str)
int compareToIgnoreCase(String str)
(6)字符串的案例
A:模拟用户登录
B:字符串遍历
C:统计字符串中大写,小写以及数字字符的个数
D:把字符串的首字母大写,其他小写
E:把int数组拼接成一个指定格式的字符串
F:字符串反转
Scanner的概述和构造方法原理
Scanner:用于接收键盘录入数据
前面的时候:
A:导包
B:创建对象
C:调用对象
System类下有一个静态的字段:
public static final InputStream in;标准的输入流对应着键盘录入
InputStream is = System.in;
class Demo{
public static final int x =10;
public static final Student s = new Student();
}
int y = Demo.x;
Student s = Demo.s;
构造方法:
Scanner(InputStream source)
Scanner类的hasNextXxx()和nextXxx()方法的讲解
/*
基本格式:
public boolean hasNextXxx():判断是否是某种类型的元素
public Xxx nextXxx():获取元素
举例:用int 类型的方法举例
public boolean hasNextInt()
public int nextInt()
注意:
InputMismatchException:输入的和你想要的不匹配
*/
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//获取数据
if(sc.hasNextInt()) {
int x =sc.nextInt();
System.out.println("x:"+x);
}else {
System.out.println("你输入的数据有误");
}
}
}
Scanner获取数据出现的小问题及解决方案
/*
常用的两个方法:
public int nextInt():获取一个int类型的值
public String nextLine():获取一个String类型的值
出现问题了:
先获取一个数值,在获取一个字符串,会出现问题。
主要原因:就是那个换行符号的问题。
如何解决呢?
A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串
B:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换成什么
*/
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//获取两个int类型的值
// if(sc.hasNextInt()) {
// int x = sc.nextInt();
// int y = sc .nextInt();
// System.out.println("x:"+ x +",y:"+y);
// }else {
// System.out.println("您输入的数据有误");
// }
//获取两个String类型的值
// String s1 = sc.nextLine();
// String s2 = sc.nextLine();
// System.out.println("s1:"+s1+",s2:"+s2);
//先获取一个字符串,在获取一个Int值
// String s1 = sc.nextLine();
// int a = sc.nextInt();
// System.out.println("s1:"+s1+",a:"+a);
//先获取一个Int值,在获取一个字符串
int a = sc.nextInt();
String s1 = sc.nextLine();
System.out.println("a:"+a+",s1:"+s1);
}
}
String类概述
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
构造方法:
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
通过查看API,我们可以知道
A:字符串字面值“abc”也可以看成是一个字符串对象
B:字符串是常量,一旦被复制,就不能被改变
String类的构造方法
//public String():空构造
//public String(byte[] bytes):把字节数组转成字符串
//public String(byte[] bytes,int offset,int length):把字节数组的一部分转成字符串
//public String(char[] value):把字符数组转成字符串
//public String(char[] value,int offset,int count):把字符数组的一部分转换成字符串
//public String(String original)把字符串常量值转换成字符串
//字符串的方法:
// public int length():返回此字符串的长度
public class StringDemo {
public static void main(String[] args) {
//public String();空构造
String s1 = new String();
System.out.println("s1:"+s1);//代表使用的是以及重写过的toString方法
System.out.println("s1.length():"+s1.length());
System.out.println("----------------------");
//public String(byte[] bytes):把字节数组转成字符串
byte[] bys = {97,98,99,45,46,44};
String s2 = new String(bys);
System.out.println("s2:"+s2);//打印的是ASCII码表的值
System.out.println("s2.length():"+s2.length());
System.out.println("---------------------");
//public String(byte[] bytes,int offset,int length):把字节数组的一部分转成字符串
String s3 = new String(bys,1,3);//从几开始几个
System.out.println("s3:"+s3);
System.out.println("s3.length()"+s3.length());
System.out.println("----------------------");
//public String(char[] value):把字符数组转成字符串
char[] chs= {'a','b','c','v'};
String s4 = new String(chs);
System.out.println("s4:"+s4);
System.out.println("s4.length()"+s4.length());
System.out.println("-----------------------------");
//public String(char[] value,int offset,int count):把字符数组的一部分转换成字符串
String s5 = new String(chs,0,3);//从0开始三个
System.out.println("s5:"+s5);
System.out.println("s5.length()"+s5.length());
System.out.println("-----------------------------");
//public String(String original)把字符串常量值转换成字符串
String s6 = new String("abced");
System.out.println("s6:"+s6);
System.out.println("s6.length()"+s6.length());
System.out.println("-----------------------------");
//字符串字面值"abc"也可以看程是一个字符串对象
String s7 = "abced";
System.out.println("s7:"+s6);
System.out.println("s7.length()"+s6.length());
}
}
//打印的的结果
s1:
s1.length():0
----------------------
s2:abc-.,
s2.length():6
---------------------
s3:bc-
s3.length()3
----------------------
s4:abcv
s4.length()4
-----------------------------
s5:abc
s5.length()3
-----------------------------
s6:abced
s6.length()5
-----------------------------
s7:abced
s7.length()5
String类的判断功能
/*
判断功能:
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空
注意:
字符串内容为空,和字符串对象为空。
String s = "";
String s = null;
*/
public class StringDemo {
public static void main(String[] args) {
//创建字符串对象
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "Helloworld";
//boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System.out.println("equals:"+ s1.equals(s2));
System.out.println("equals:"+ s1.equals(s3));
System.out.println("----------------------");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("equals:"+ s1.equalsIgnoreCase(s2));
System.out.println("equals:"+ s1.equalsIgnoreCase(s3));
System.out.println("----------------------");
//boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println("contains:"+ s1.contains("hello"));
System.out.println("contains:"+ s1.contains("hw"));//不包含,因为判断是否包含必须是连着的字符串
System.out.println("----------------------");
//boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System.out.println("startsWith:"+ s1.startsWith("h"));
System.out.println("startsWith:"+ s1.startsWith("l"));
System.out.println("----------------------");
//boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
System.out.println("endsWith:" +s1.endsWith("d"));
System.out.println("endsWith:" +s1.endsWith("w"));
System.out.println("----------------------");
//boolean isEmpty():判断字符串是否为空
System.out.println("isEmpty:" +s1.isEmpty());
String s4 = "";
String s5 = null;
System.out.println("isEmpty:" +s4.isEmpty());
//NullPointerException
//s5对象都不存在,所以不能调用方法
//System.out.println("isEmpty:" +s5.isEmpty());
}
}
模拟登录
import java.util.Scanner;
public class DengluJieMian {
public static void main(String[] args) {
String username = "admin";
String password = "admin";
for(int x=2;x>-1;x--) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入用户名:");
String user = sc.next();
System.out.println("请输入密码:");
String pass = sc.next();
if(user.equals(username)&&pass.equals(password)) {
System.out.println("恭喜您,登录成功");
break;
}else {
if(x ==0) {
System.out.println("次数已经用完,请与管理员联系");
}else {
System.out.println("输入错误,你还有"+x+"次机会");
}
}
}}
}
String类的获取功能
/*
String类的获取功能
int length():获取字符串的长度
char charAt(int index):获取指定索引位置的字符
int indexof(int ch):返回指定字符在此字符串中第一次出现处的索引
为什么这里是int类型,而不是char类型?
原因是:'a'和97其实都可以代表'a'
int indexof(String str):返回指定字符串在此字符串中第一次出现处的索引
int indexof(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
String substring(int start):从指定位置开始截取字符串,默认到结尾
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
*/
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串对象
String s = "helloworld";
//int length():获取字符串的长度
System.out.println("s.length:" +s.length());
System.out.println("---------------");
//char charAt(int index):获取指定索引位置的字符
System.out.println("charAt:" +s.charAt(4));
System.out.println("---------------");
//int indexof(int ch):返回指定字符在此字符串中第一次出现处的索引
System.out.println("indexof:"+s.indexOf('l'));
System.out.println("---------------");
//int indexof(String str):返回指定字符串在此字符串中第一次出现处的索引
System.out.println("indexof:"+s.indexOf("hello"));
System.out.println("---------------");
//int indexof(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
System.out.println("indexof:"+ s.indexOf('l', 4));
System.out.println("---------------");
//String substring(int start):从指定位置开始截取字符串,默认到结尾
System.out.println("substring:"+ s.substring(2));
System.out.println("---------------");
//String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
System.out.println("substring:" +s.substring(2, 6));
System.out.println("---------------");
}
}
字符串的遍历
/*
获取每一个字符
*/
public static void main(String[] args){
String s = "helloworld";
for(int x =0;x<s.length();x++){
System.out.println(s.charAt(x));
}
}
统计大小写及数字字符的个数案例
/*
找出字符串里面的大写小写数字各有几个:
举例:"HelloWorld123"
结构:
大写字符:2个
小写字符:8个
数字字符:3个
分析:
A:定义三个统计变量
bigCount = 0;
smallCount = 0;
numberCount = 0;
B:遍历字符串,得到每一个字符
length()和charAt结合
C:判断该字符到底是属于哪种类型的
大:bigCount++
小:smallCount++
数字:numberCount++
如何判断某个字符是大写还是小写还是数字
char ch = s.charAt(x);
if(ch>='0' && ch<='9')numberCount++
if(ch>='a' && ch<='z')smallCount++
if(ch>='A' && ch<='Z')bigCount++
*/
public class StringTest2 {
public static void main(String[] args) {
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
//创建对象
String s ="HelloWorld123";
for(int x = 0;x<s.length();x++) {
char ch = s.charAt(x);
//判断该字符到底是属于哪种类型的
if(ch>='0' && ch<='9') {
numberCount++;
}
if(ch>='a' && ch<='z') {
smallCount++;
}
if(ch>='A' && ch<='Z') {
bigCount++;
}
}
System.out.println("大写字符有:"+ bigCount+"个");
System.out.println("小写字符有:"+ smallCount+"个");
System.out.println("数字字符有:"+ numberCount+"个");
}
}
String类的转换功能
/*
String的转换功能:
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 class StringDemo {
public static void main(String[] args) {
//定义一个字符串对象
String s = "JavaSE";
//byte[] getBytes():把字符串转换为字节数组。
byte[] bys = s.getBytes();
for(int x = 0; x <bys.length;x++) {
System.out.println(bys[x]);
}
System.out.println("----------------");
//char[] toCharArray():把字符串转化为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x<chs.length;x++) {
System.out.println(chs[x]);
}
System.out.println("---------------");
//static String valueOf(char[] chs):把字符数组转成字符串
String ss = String.valueOf(chs);
System.out.println(ss);
System.out.println("------------------");
//static String valueOf(int i):把int类型的数据转换成字符串
int i =100;
String s1 = String.valueOf(i);
System.out.println(s1);
System.out.println("------------------");
//String toLowerCase():把字符串转成小写
String xx = "JAVASE";
String xxx = xx.toLowerCase();
System.out.println(xxx);
System.out.println("------------------");
//String toUpperCase():把字符串转成大写
String x1 = xxx.toUpperCase();
System.out.println(x1);
System.out.println("------------------");
//String concat(String str):把字符串拼接
String sw = "hello";
String sw2 = "world";
String sw3 = sw+sw2;
String sw4 = sw.concat(sw2);
System.out.println(sw4);
System.out.println(sw3);
}
}
把字符串的首字母转大写其他小写
/*
需求: 将第一个字母大写其他小写
*/
public class StringTest {
public static void main(String[] args) {
//创建对象
String s = "helloWORLD";
//第一步:将全部字符转小写
String s1 = s.toLowerCase();
//System.out.println(s1);
//截取第一个字符
String s2 = s1.substring(0,1);
//System.out.println(s2);
//截取除了第一个其他字符
String s3 =s1.substring(1);
//把s3转换成大写
String s4 = s2.toUpperCase();
//拼接s3+s4;
String s5 = s4.concat(s3);
System.out.println(s5);
//优化后的代码:
//链式编程
String result = (s.substring(0, 1).toUpperCase())
.concat(s.substring(1).toLowerCase());
System.out.println(result);
}
}
String类的其他功能
/*
String类的其他功能
替换功能:replace:替换
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符:
int compareTo(String str)
int compareToIgnoreCase(String str)
*/
public class StringDemo {
public static void main(String[] args) {
//String replace(char old,char new)
String s = "helloworld";
String s1 = s.replace('h', 'H');
System.out.println(s);
System.out.println(s1);
System.out.println("--------------------");
//String replace(String old,String new)
String s3 = s.replace("hello", "qwq");
System.out.println(s3);
System.out.println("---------------------");
//String trim()
String s4 = " hello world" ;//去除的是两端空格
String s5 = s4.trim();
System.out.println(s5+"***");
System.out.println("--------------------");
//int compareTo(String str)(区分大小写 )
String s6 = "adawd ";
String s7 = "bdsadsaddsdf";
String s8 = "Hdeada";
int i = s6.compareTo(s7);//ASCII码表,a-b=-1;
int i1 = s6.compareTo(s8);
System.out.println(i);
System.out.println(i1);
System.out.println("--------------------");
//int compareToIgnoreCase(String str)(不区分大小写)
String s9 = "addsd";
String s10 = "Heafdafa";
System.out.println(s9.compareToIgnoreCase(s10));
}
}
把Int数组拼接字符串的案例
/*
需求:把数组中的数据按照指定格式拼接成一个字符串
举例;
int[] arr = {1,2,3};
输出结果:
"[1,2,3]"
分析:
A:定义一个字符串对象,只不过内容为空
B:先把字符串拼接一个"["
C:遍历int数组,得到每个元素
D:先判断该元素是否为最后一个
是:就直接拼接元素和"]"
不是:就拼接元素和逗号以及空格
E:输出拼接后的字符串
*/
public class StringTest {
public static void main(String[] args) {
//提前是数组已经存在
int[] arr = {1,2,3};
//定义一个字符串对象,只不过内容为空
String s ="";
//先把字符串拼接一个"["
s +="[";
//遍历int数组
for(int x = 0; x<arr.length;x++) {
//先判断是否是最后一个元素
if(x == arr.length - 1) {
//就直接拼接元素和"]"
s += arr[x];
s += "]";
}else {
//就拼接元素和逗号以及空格
s += arr[x];
s += ", ";
}
}
//输出拼接后的字符
System.out.println("最终的字符串是:" + s);
}
}
字符串反转的案例
/*
字符串反转
举例:键盘录入"abc"
输出结果"cba"
分析:
A:键盘录入一个字符串
B:定义一个新字符串
C:到这遍历字符串,得到每一个字符
D:用新字符串把每一个字符拼接起来
E:输出新字符串
*/
import java.util.Scanner;
public class StringDemo {
public static void main(String[] args) {
//键盘录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
/*
方法一
//定义新的字符串
String result = "";
//把字符串转换成字符数组
char[] chs = line.toCharArray();
//到这遍历字符串,得到每一个字符
for(int x =chs.length - 1; x>= 0;x--) {
//用新字符串把每一个字符拼接起来
result += chs[x];
}
//输出新字符串
System.out.println("倒着输出的结果:"+result);
*/
//改进为功能实现
String s =myReverse(line);
System.out.println("反转后的结构:"+s);
}
/*
两个明确:
返回值类型:String
参数列表:String
*/
//方法二
public static String myReverse(String s) {
//定义新的字符串
String result = "";
//把字符串转换成字符数组
char[] chs = s.toCharArray();
//到这遍历字符串,得到每一个字符
for(int x =chs.length - 1; x>= 0;x--) {
//用新字符串把每一个字符拼接起来
result += chs[x];
}
return result;
}
}