JAVA学习笔记-----Eleven(scanner ,String)

一.Scanner

1.Scanner的概述和构造方法原理
A:Scanner的概述: JDK5以后用于获取用户的键盘输入
B:Scanner的构造方法原理
Scanner(InputStream source)
System类下有一个静态的字段:
public static final InputStream in; 标准的输入流,对应着键盘录入。
2.Scanner类的hasNextXxx()和nextXxx()方法
A:基本格式
hasNextXxx() 判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
如果需要判断是否包含下一个字符串,则可以省略Xxx
nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
3.Scanner获取数据
A:两个常用的方法:
​ public int nextInt():获取一个int类型的值
​ public String nextLine():获取一个String类型的值
​ public String next():获取一个String类型的值

案例演示:

 Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int i = sc.nextInt();
        sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s = sc.nextLine();
        System.out.println(s);


二.String类

1.String类的概述
A:什么是字符串
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
B:String类的概述
通过JDK提供的API,查看String类的说明
可以看到这样的两句话。
a:字符串字面值"abc"也可以看成是一个字符串对象。
b:字符串是常量,一旦被创建,就不能被改变。
2.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):把字符串常量值转成字符串
3.String的特点
A:String的特点: 一旦被创建就不能改变 因为字符串的值是在方法区的常量池中划分空间 分配地址值的
4.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(): 判断字符串的内容是否为空串""。

        String s5 = "hello";
        String s6 = "hello";
        System.out.println(s5 == s6);//true
        System.out.println(s5.equals(s6)); //true

 String s="";//定义一个空串
        boolean empty = s.isEmpty();
        System.out.println(empty);

        if(s.length()==0){
            System.out.println("是空串");
        }else{
            System.out.println("不是空串");
        }

5.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): 从指定位置开始到指定位置结束截取字符串。

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

6.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 s = "aaAbdfesefAAfefefe";
        //链式编程
        s = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
        System.out.println(s);

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

案例演示:

需求:模拟登录, 给三次机会, 并提示还有几次。

        String name="cl";
        String password="233333";
        for (int i = 2; i >=0; i--) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的用户名");
        String uName = scanner.nextLine();
        System.out.println("请输入你的密码");
        String pPwd = scanner.nextLine();
        if(uName.equals(name)&&pPwd.equals(password)){
            System.out.println("恭喜你登陆成功!");
            break;
        }else{
            System.out.println("登录失败!你还有" + i + "次机会!");
        };

        }
public class Test6 {
//    键盘输入:abcde
//    打印输出:edcba
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串");
        String a = scanner.nextLine();
        for (int i = a.length()-1; i>=0  ; i--) {
            System.out.print(a.charAt(i));
        }
    }
}
public class Test7 {
//    请编写程序,统计键盘录入的字符串中包含大写字母、小写字母、数字的个数,并测试。
    public static void main(String[] args) {
        int a=0;
        int b=0;
        int c=0;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入字符串:");
        String sc=scanner.nextLine();
        for (int i = 0; i <sc.length() ; i++) {
            char ch=sc.charAt(i);
            if (ch>='a'&&ch<='z'){
                a++;
            }else if (ch>='A'&&ch<='Z'){
                b++;
            }else if(ch>='0'&&ch<='9'){
                c++;
            }
        }
        System.out.println("字符串中小写字母的个数为:"+a+"个。");
        System.out.println("字符串中大写字母的个数为:"+b+"个。");
        System.out.println("字符串中数字的个数为:"+c+"个。");
    }
}
public class Test8 {
//    请编写程序,将键盘录入的字符串中敏感字符过滤掉,并测试。
//    例: 敏感字: 奥巴马、普京
//    键盘输入: 我喜欢奥巴马呀,还喜欢普京
//    打印输出: 我喜欢*呀,还喜欢*
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入字符串:");
        String sc=scanner.nextLine();
        String sc1 =sc.replace("奥巴马","*").replace("普京","*");
        System.out.println(sc1);
    }
}
public class Test9 {
//    请编写程序,统计键盘录入的字符串中出现了几次字符串”java”,并测试
//    例:  键盘输入:woyaoxuejava,xihuanjava,aijava,javajavawozuiai
    public static void main(String[] args) {
        int count=0;
        String b="java";
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入字符串:");
        String sc=scanner.nextLine();
            int a=sc.indexOf(b);
            while( a !=-1) {
                    count++;
                a = sc.indexOf(b, a + 4);
        }
       System.out.println(count);
    }
}

这一块儿的学习,需要多敲代码,只有熟练使用其中的方法调用,才能记住这些代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值