常用类的学习
Scanner类
-
一个简单的文本扫描器,可以用正则表达式解析原始类型和字符串
-
数据Java.until包,所以在使用时需要导包。
-
scanner有既可以录入信息,也可以对录入的信息判断是否合理。
- 录入:next xx()方法
- nextInt();录入int类型
- nextByte();录入byte类型
- nextFloat();录入float类型
- nextDouble();录入double类型
- nextline();录入字符串类型
- 判断:hasNext xx()方法
- hasNextInt();判断是否录入int类型
- hasNextByte();判断是否录入byte类型
- hasNextFloat();判断是否录入float类型
- hasNextDouble();判断是否录入double类型
- hasNextline();判断是否录入line类型
- 录入:next xx()方法
-
容易出错点
当我们先录入一个整数,在录入一个字符串是,会出现字符串没让输入,程序就结束了。
示例一: public class demo0427 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); int i = sc.nextInt(); System.out.println(i); System.out.println("请输入一个字符串"); String line = sc.nextLine(); System.out.println(line); } } F:\ruanjian\JDK\jdk-13.0.2\bin\java.exe "-javaagent:F:\ruanjian\IEDA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51480:F:\ruanjian\IEDA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath F:\untitled\westos.com.demo04x\out\production\westos.com.demo04x daly.demo0427 请输入一个整数 12 12 请输入一个字符串 Process finished with exit code 0 程序没有录入字符串就运行结束了,其实程序录入了字符串,只是程序把你的回车当成字符串录入了 解决方法:录入时新创建一个新的对象/调用新的录入方法next(); public class demo0427 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); int i = sc.nextInt(); System.out.println(i); System.out.println("请输入一个字符串"); // 方法1: String line = sc.next(); // 方法2: /* Scanner sc2 = new Scanner(System.in); System.out.println("请输入一个字符串"); String line = sc2.nextLine();*/ System.out.println(line); } } 运行结果: F:\ruanjian\JDK\jdk-13.0.2\bin\java.exe "-javaagent:F:\ruanjian\IEDA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51932:F:\ruanjian\IEDA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath F:\untitled\westos.com.demo04x\out\production\westos.com.demo04x daly.demo0427 请输入一个整数 12 12 请输入一个字符串 12 er 12 //录入的字符串中有空格,就结束录入,导致录入不完整 Process finished with exit code 0 //方法2有一个问题,当字符串之间有空格或者回车时,就结束录入,出现录入不完整。 示例二: public class Test3 { public static void main(String[] args) { System.out.println("请输入一个整数"); int num = ScannerTool.tool(); System.out.println(num); } } 工具包: //利用Scanner输入int类型的信息,判断输入是否正确的工具包 import java.util.Scanner; public class ScannerTool { // 将构造方法私有化,调用时不能创建对象,隐藏细节。 private ScannerTool() { } //构造方法私有化了,不能创建对象,所以将方法静态化,直接类名调方法。 public static int tool() { int num; while (true) { Scanner sc = new Scanner(System.in); boolean b = sc.hasNextInt(); if (b) { int i = sc.nextInt(); num=i; sc.close(); break; } else { System.out.println("请你重新正确输入整数"); } } return num; } }
String类的概述
-
概念:字符串:字符串是由多个字符组成的数据(字符序列)。
-
字符串可以看成是字符数组()。
-
字符串是常量,一旦被创建就不能改变
字符串字面值“123”,也可以被看作是一个对象创建。
public class Test1 { public static void main(String[] args) { String s="123"; System.out.println(s); String s1 = new String("abc"); System.out.println(s1); } }
内存图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yHehhFz6-1587973267646)(https://pic.downk.cc/item/5ea655c5c2a9a83be5e8acd0.png)]
字符串是常量,一旦被创建就不能改变,指的是字符串在字符串常量池中创建,下次在创建时会先在常量池中寻找是否有,有就使用,没有再创建。
-
常见的构造方法
- public String():空构造
- public String(String original):把字符串常量值转成字符串
- 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 class Test4 {
public static void main(String[] args) {
// 将字节数组,转化成字符串,每个转成对应的字符
byte[]arr={48,65,97,68,69};
String s = new String(arr);
System.out.println(s);
// 从指定位置处和长度,转化成字符串。
String s1 = new String(arr,1,3);
System.out.println(s1);
String s2 = new String(arr,0,2);
System.out.println(s2);
// 把字符数组转化成字符串
char [] arr2={'a','b','A','B'};
String s3 = new String(arr2);
System.out.println(s3);
String s4 = new String(arr2,1,2);
System.out.println(s4);
}
看程序写结果:
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); //false new了两个空间
System.out.println(s1.equals(s2)); //ture 内容相同
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4); //false 指向不同
System.out.println(s3.equals(s4)); //ture 内容相同
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);//ture 都指向堆内存的字符串常量池
System.out.println(s5.equals(s6));//ture 内容相同
注意:String 类重写了toString 方法(输出字符串的内容,而不是地址值)。也重写了equals方法(不再比较地址值是否相同,而是比较字面内容是否相同)