Java常用类之Scanner
Scanner此类常用于获取用户的键盘录入的数据,此类下的许多方法都是对键盘录入的数据进行操作,这是我们Java语言实现与用户交互的重要工具类;
Scanner类的hasNextXXX()方法和nextXXX()方法
两种方法的基本格式
hasNextxxx()是判断下一个键盘录入的是否是某种类型的元素,其中的xxx可以是Int,doubl型。如果要判断是否包含下一个字符串,则可以省略xxx,next()获取下一个输入项,同样这个方法的xxx可以是Int,Double型。我们可以通过这两个键盘录入的方法来进行用户输入信息的限定:
例如:我们要求用户键盘录入的信息是整型数据,不能是double型的
class test
{
public static void main(String[] args) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整型数据");
boolean b = sc.hasNextInt();
if (b) {
int a = sc.nextInt();
break;
} else {
System.out.println("类型错误,请重新输入数据");
}
}
}
}
输出结果:
这样就达到了我们对录入数据进行判断的目的,如果不是我们需要的数据类型,我们就可以再次进行重新录入。这里我们还需要注意一个小问题,那就是我们的Scanner在录入数据的时候,我们后台程序的编写需要注意的一个非常细致的小问题。
下来进行多个int型值的录入,再进行多个String型的数据进行录入,然后,我们在进行先int型再String型的数据录入,我们来看看会出现什么问题;
class test
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个int型数据");
int a=sc.nextInt();
System.out.println("请输入第二个int型数据");
int b=sc.nextInt();
System.out.println("---------------------------");
System.out.println("请输入第一个String型数据");
String c=sc.nextLine();
System.out.println("请输入第二个String型的数据");
String d=sc.nextLine();
System.out.println("-----------------------------");
System.out.println("请输入第一个int型的数据");
int e=sc.nextInt();
System.out.println("请输入第二个String型的数据");
String f=sc.nextLine();
}
}
输出结果:
观察结果我们可以看出,在这段代码的执行窗口,我们进行数据的键盘录入的时候,在进行 录入Int数据类型结束的时候,录入第一String类型的时候,控制台没有停下来让我们进行数据的录入,而是直接的执行到了第二个String数据类型的录入,这是因为,在录入int整型结束的时候,我们 敲得一下回车键,控制台认为是录入的String型数据,所以会跳过第一个String数据的录入,这种问题的解决办法由两种:1.我们们可以在获取完一种数据类型之后,再创建一个新的键盘录入对象来进行信息录入;2.我们可以先将所有的数据都先按照String型的数据录入,然后再将录入的数据转换位想要的数据类型;接下来我们使用第一种方法来看看:
package worktest_4_24;
import java.util.Scanner;
class test
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个int型数据");
int a=sc.nextInt();
System.out.println("请输入第二个int型数据");
int b=sc.nextInt();
System.out.println("---------------------------");
sc = new Scanner(System.in);
System.out.println("请输入第一个String型数据");
String c=sc.nextLine();
System.out.println("请输入第二个String型的数据");
String d=sc.nextLine();
System.out.println("-----------------------------");
sc = new Scanner(System.in);
System.out.println("请输入第一个int型的数据");
int e=sc.nextInt();
sc = new Scanner(System.in);
System.out.println("请输入第二个String型的数据");
String f=sc.nextLine();
}
}
看一下运行的结果是否正常:
可见这样运行的结果是正常的。
Java常用类String类的使用
关于这个类,我们经常接触过,因为我们再定义一个类的成员变量的时候 ,经常使用到字符串数据类型,我们都知道字符串数据类型是引用数据类型,他是用对象指向堆内存的内存空间地址来进行数据的表示,而这个String,是一个类名,而每一个字符串他就是一个字符串对象,可以理解是String类的一个实例对象,这里我们需要注意到我们可以直接写“15h”这也可以看成一个String类型的实例对象,字符串是一个常量,一但被创建,就不能被更改这个的理解我们可以来看一个实例:
class test
{
public static void main(String[] args) {
String s = "hello" ;
s = "world" + "hello";
System.out.println(s);
}
}
输出结果:
结果我们可以看到,s的内容发生了变化,这个和之前的字符串是一个常量,一但被创建,就不能被更改这个是不是有所冲突啊?其实实际上并不是我们表面上理解的这样,这句话没错,一个字符串一但被创建了,其值是不能够再被改变的,但是,s并不是这个“hello”这个字符串,s只是这个字符串空间的一个引用,它还可以被重新赋值,而“hello”存放空间的内容是没有办法再改变的我们可以通过下图理解一下:
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):把字符串常量值转成字符串
class tset{
public static void main(String[] args) {
byte[] a = {55, 56, 57, 28};
String str1=new String(a);
String str2=new String(a,2,1);
char b[]={'字','符','数','组'};
String str3=new String(b);
String str4=new String(b,1,2);
String str5="字符串常量";
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
}
输出结果;
以上就是我们通过不同的构造方法来进行String类型的对象建立方法;
String类中的常用判断功能方法
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(): 判断字符串的内容是否为空串""。
class tset{
public static void main(String[] args) {
String str1="大大大A";
String str2="小小小a";
String str3="大大大a";
boolean equals1 = str1.equals(str2);
boolean equals2= str1.equalsIgnoreCase(str3);
boolean equals3 = str1.contains("大");
boolean equals4 = str2.startsWith("小");
boolean equals5 = str2.startsWith("a");
boolean equals6 = str1.isEmpty();
System.out.println(equals1);
System.out.println(equals2);
System.out.println(equals3);
System.out.println(equals4);
System.out.println(equals5);
System.out.println(equals6);
}
}
输出结果:
以上就是对String类的判断功能。
String类的获取功能方法
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): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
可以顺带提一下lastIndexOf系列
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束。
String类的转换功能
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): 把字符串拼接。
以上两种对String类的字符串操作的的方法很多,方法很多,关键在于我们合理灵活的去应用,我们现在就用以上的两类方法来实现字符串数组的遍历(这里分别用字符串截取和转换字符数组的方法)
-
字符串截取的方法
class tset{ public static void main(String[] args) { System.out.println("请输入一个字符串"); Scanner sc = new Scanner(System.in); String str=sc.nextLine(); for (int i = 0; i <=str.length() ; i++) { String str1; if (i == str.length()) { str1 = str.substring(str.length() - 1); break; } else { str1 = str.substring(i, i + 1); System.out.println(str1); } } } }
输出结果:
-
转换成字符数组再进行遍历
class tset{ public static void main(String[] args) { System.out.println("请输入一个字符串"); Scanner sc = new Scanner(System.in); String str=sc.nextLine(); char[] char1=str.toCharArray(); for (int i = 0; i <str.length() ; i++) { System.out.print(char1[i]+" "); } } }
输出结果:
如上,我们使用了两种方法来进行一个键入字符串的遍历。
String类的其他功能
String的替换功能及案例演示
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
B:String的去除字符串两空格及案例演示
public String trim() 去除两端空格
C:String的按字典顺序比较两个字符串及案例演示
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
String类中在大串中查找小串的出现次数
例如:我们要统计jjhgaiugfiuguiagufnaklgu中,“gu”出现的次数
class tset{
public static void main(String[] args) {
String str1="jjhgaiugfiuguiagufnaklgu";
String str2="gu";
int num=0;
int indext=str1.indexOf(str2);
while(indext!=-1){
num++;
str1=str1.substring(indext+str2.length());
indext=str1.indexOf(str2);
}
System.out.println(num);
}
}
输出结果:
,这样就实现了在大串中查找小串的目的;当然这个需求还能够用另一种方法进行实现
class tset {
public static void main(String[] args) {
String str1 = "jjhgaiugfiuguiagufnaklgu";
String str2 = "gu";
String str3 = str1.replace(str2, "");
int num = (str1.length() - str3.length()) / 2;
System.out.println(num);
}
}
输出结果:
我们通过替换大串里的小串为一个空串,然后将两个字符串的长度做差值再与小串的长度做比值,这样就可以实现查询小串的次数了。