Java--常见对象--Scanne、 String

Scanner(用于接收键盘录入数据)
String(字符串)

常见对象(Scanner的概述和构造方法原理)

A:Scanner的概述:	JDK5以后用于获取用户的键盘输入
B:Scanner的构造方法原理
	Scanner(InputStream source)
	System类下有一个静态的字段:
		public static final InputStream in; 标准的输入流,对应着键盘录入。

常见对象(Scanner类的hasNextXxx()和nextXxx()方法)

A:基本格式
	hasNextXxx()  判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
				  如果需要判断是否包含下一个字符串,则可以省略Xxx
	nextXxx()  获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
B:案例演示
	演示结合判断获取数据的情况
public class MyTest {
    public static void main(String[] args) {

        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个整数");
            if (sc.hasNextInt()){
                int num=sc.nextInt();
                System.out.println(num);
                break;
            }else{
                System.out.println("输入不正确,请重新输入");
            }
        }

    }
}
	

常见对象(Scanner获取数据出现的小问题及解决方案)

​	A:两个常用的方法:
​		public int nextInt():获取一个int类型的值
​		public String nextLine():获取一个String类型的值
​		public String next():获取一个String类型的值

常见对象(String类的概述)

A:什么是字符串
	字符串是由多个字符组成的一串数据(字符序列)
	字符串可以看成是字符数组
B:String类的概述	
	通过JDK提供的API,查看String类的说明
	
	可以看到这样的两句话。
	a:字符串字面值"abc"也可以看成是一个字符串对象。
	b:字符串是常量,一旦被创建,就不能被改变。

常见对象(String类的构造方法和String的特点)

A:常见构造方法
	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):把字符串常量值转成字符串
B:案例演示	
	演示String类的常见构造方法
	public class MyTest {
    public static void main(String[] args) {
        String string = new String();
        System.out.println(string);

        String string1 = new String("dytct");
        System.out.println(string1);

        byte[] bytes = {100, 15, 55, 66, 103};

        String s = new String(bytes);
        System.out.println(s);
        String s1 = new String(bytes, 0, 3);
        System.out.println(s1);

        char[] chars={'a','c','d','k','u','好','好','学','习'};
        String s2 = new String(chars);
        System.out.println(s2);

        String s3 = new String(chars,5,4);
        System.out.println(s3);
    }
}

	演示前先说一个字符串的方法:
		public int length():返回此字符串的长度。
		public class MyTest {
    public static void main(String[] args) {
        String s = new String("vsauycg");
        int length=s.length();
        System.out.println(length);
    }
}
 C:String的特点:	一旦被创建就不能改变 因为字符串的值是在方法区的常量池中划分空间 分配地址值的
 D:==和equals()的区别
    ==是一个比较运算符,可以比较基本类型,也可以比较引用类型。
    ==比较的是基本类型,比的是两个值是否相等。
    ==比较引用类型,比的是两个地址值是否相等。
    equals()是Object类中的一个方法,只能比较引用类型,默认比较的是两个对象的地址值是否相同
    很多类会重写 equals()方法,会按照重写过后的方式去比较。

常见对象(String类的判断功能)

A: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():						判断字符串的内容是否为空串""。
B:案例演示:	案例演示String类的判断功能;
public class MyTest {
    public static void main(String[] args) {

        boolean g = "gahgyu".equalsIgnoreCase("GAHGYU");
        System.out.println(g);

        boolean s = "好好学习,天天向上".contains("天天向上");
        System.out.println(s);

        boolean d = "赵一一".startsWith("赵");
        System.out.println(d);

        boolean b = "赵一一".endsWith("一");
        System.out.println(b);

        String l="";
        System.out.println(l.isEmpty());

        if (l.length()==0){
            System.out.println("空串");
        }

    }
}

常见对象(模拟用户登录)

A:案例演示:	需求:模拟登录,给三次机会,并提示还有几次。
public class MyTest {
    public static void main(String[] args) {
        String username="赵一";
        String password="123456";
        for (int i = 1; i <=3; i++) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的用户名");
            String name=scanner.nextLine();

            System.out.println("请输入你的密码");
            String pwd=scanner.nextLine();
            if (username.equals(name)&&password.equals(pwd)){
                System.out.println("登录成功");
                break;
            }else {
                if ((3-i)==0) {
                    System.out.println("你的次数已用完");
                }else {
                    System.out.println("用户名或密码输入错误,请重新输入");
                    System.out.println("你还剩余"+(3-i)+"次");
                }

            }


        }
    }
}

常见对象(String类的获取功能)

A: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):	从指定位置开始到指定位置结束截取字符串。
B:案例演示
	案例演示String类的获取功能
	public class MyTest {
    public static void main(String[] args) {

        System.out.println("biabci".length());

        String s="一行白鹭上青天";
        char c = s.charAt(s.length()-1);
        System.out.println(c);

        int q = "我还是从前那个少年,没有一丝丝改变".indexOf("是");
        System.out.println(q);

        int indexOf = "我还是从前那个少年,没有一丝丝改变".indexOf("少年");
        System.out.println(indexOf);

        String p="我还是从前那个少年,没有一丝丝改变";
        int l = p.indexOf("丝", p.indexOf("丝") + 1);
        System.out.println(l);

        int k = p.lastIndexOf("有");
        System.out.println(k);

        String substring = "我还是从前那个少年,没有一丝丝改变".substring(3);
        System.out.println(substring);

        String substring1 = "我还是从前那个少年,没有一丝丝改变".substring(5, 9);
        System.out.println(substring1);

    }
}

常见对象(字符串的遍历)

A:案例演示:	需求:遍历字符串
public class MyTest {
    public static void main(String[] args) {
        String s="我还是从前那个少年,没有一丝丝改变";
        for (int i = 0; i < s.length(); i++) {
            char c=s.charAt(i);
            System.out.println(c);
        }
        System.out.println("========================");
        for (int i=s.length()-1;i>=0;i--){
            char c=s.charAt(i);
            System.out.println(c);
        }
    }
}

常见对象(统计不同类型字符个数)(掌握)

A:案例演示:	需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public class MyTest {
    public static void main(String[] args) {
        String str="asknuyeyhaooioSAGUYGAUC7854xtrwlkun";
        int x=0;
        int d=0;
        int num=0;

        for (int i = 0; i < str.length(); i++) {
            char ch=str.charAt(i);
            if (ch>='a'&&ch<='z'){
                x++;
            }else if(ch>='A'&&ch<='Z'){
                d++;
            }else {
                num++;
            }
        }
        System.out.println("小写字母"+x);
        System.out.println("大写字母"+d);
        System.out.println("数字"+num);
    }
}

常见对象(String类的转换功能)

A: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):					把字符串拼接。
B:案例演示
	案例演示String类的转换功能
1.public class MyTest {
    public static void main(String[] args) {
        byte[] bytes = "asldkj".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        String s = new String(bytes);
        System.out.println(s);

        byte[] bytes1 = "你好".getBytes();
        System.out.println(bytes.length);

        String b="好好学习,天天向上";
        char[] chars = b.toCharArray();
        System.out.println(Arrays.toString(chars));

        String s1 = new String(chars);
        System.out.println(s1);

        String s2 = new String(chars, 0, s1.indexOf('天') + 1);
        System.out.println(s2);
    }
}
2.	public class MyTest {
    public static void main(String[] args) {
        int num=100;
        String str=num+"";

        boolean b=true;
        String str2=b+"";

        String s = String.valueOf(200);
        System.out.println(s);

        String s1 = String.valueOf(new char[]{'a', 'c', 'h', 'u'});
        System.out.println(s1);

        String s2 = "ascd".toUpperCase();
        System.out.println(s2);

        String s3 = "SASOIOJ".toLowerCase();
        System.out.println(s3);

        String s4 = "dihiuhd".concat("okaloi");
        System.out.println(s4);
    }
}

常见对象(按要求转换字符)

A:案例演示:	需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
public class MyTest3 {
    public static void main(String[] args) {
        String str="apdksjDAIOJDOiMKLDSJEksnacans";
       /* String concat = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
        System.out.println(concat);*/
        String substring = str.substring(0, 1);
        String s = substring.toUpperCase();

        String substring1 = str.substring(1);
        String s1 = substring1.toLowerCase();
        String concat=s.concat(s1);

        System.out.println(concat);
    }
}

常见对象(String类的其他功能)

A: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) 跟上面一样 只是忽略大小写的比较 

常见对象(把数组转成字符串)

A:案例演示
	需求:把数组中的数据按照指定个格式拼接成一个字符串
		举例:
			int[] arr = {1,2,3};	
		拼接结果:
			"[1, 2, 3]"
public class MyTest {
    public static void main(String[] args) {
        int[] arr={1,2,3};

        String str="[";
        for (int i = 0; i < arr.length; i++) {
            if (i==arr.length-1){
                str+=arr[i]+"]";
            }else {
                str+=arr[i]+",";
            }
        }
        System.out.println(str);
    }

}

常见对象(字符串反转并断点查看)

A:案例演示
	需求:把字符串反转
		举例:键盘录入"abc"		
		反转结果:"cba"
public class MyTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一段字符串");
        String s=scanner.nextLine();

        String str="";
        for (int i = s.length()-1; i >=0; i--) {
            str+=s.charAt(i);
        }
        System.out.println(str);
    }
}

常见对象(在大串中查找小串出现的次数思路图解)

A:画图演示
	需求:统计大串中小串出现的次数
	举例: "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次
public class MyTest {
    public static void main(String[] args) {
        String maxStr="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        int index=maxStr.indexOf("java");
        int count=0;
        while (index!=-1){
            count++;
            maxStr=maxStr.substring(index+4);
            index=maxStr.indexOf("java");
        }
        System.out.println(count);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值