B:Scanner的構造方法原理java
System類下有一個靜態的字段:面試
C:通常方法編程
import java.util.Scanner; public class Scanner_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //鍵盤錄入 System.out.println("請輸入一個整數:"); // int i = sc.nextInt(); //存儲在I中 // System.out.println(i); if(sc.hasNextInt()) { int i =sc.nextInt(); System.out.println(sc); } else { System.out.println("您輸入的類型有誤"); } } }
A:兩個經常使用的方法:數組
B:案例演示ide
c:問題解決方案優化
public class Scanner_2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* System.out.println("請輸入一個整數:"); int i1 = sc.nextInt(); System.out.println("請輸入第二個整數:"); int i2 = sc.nextInt(); System.out.println("i1 = " + i1 + ",i2 = " + i2); */ /* System.out.println("請輸入第一個字符串"); String line1 = sc.nextLine(); System.out.println("請輸入第二個字符串"); String line2 = sc.nextLine(); System.out.println("line1 = " + line1 + ",line2 = " + line2); */ System.out.println("請輸入一個整數:"); int i1 = sc.nextInt(); Scanner sc2 = new Scanner(System.in); System.out.println("請輸入第二個字符串"); String line = sc2.nextLine(); System.out.println("i1 = " + i1 + ",line = " + line); } }
A:String類的概述編碼
能夠看到這樣的兩句話。idea
public class string_1 {.net
public static void main(String[] args) { //Person p = new Person(); String str= "abc"; //"abc"能夠當作是一個字符串對象。 str= "edf"; //當把"def"賦值給str,原來的"abc"就變成了垃圾 System.out.println(str); //String類重寫了toString方法返回的是該對象的自己 }
}指針
A:常見構造方法
B:案例演示
1.判判定義爲String類型的s1和s2是否相等
2.下面這句話在內存中建立了幾個對象?
3.判判定義爲String類型的s1和s2是否相等
4.判判定義爲String類型的s1和s2是否相等
5.判判定義爲String類型的s1和s2是否相等
A:String類的判斷功能
package net.allidea.string; public class String_4 { public static void main(String[] args) { // demo1(); // demo2(); String s1 = "zheng"; String s2 = ""; String s3 = null; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); System.out.println(s3.isEmpty()); //java.lang.NullPointerException } private static void demo2() { String s1 = "歡樂啦啦啦,嘿嘿"; String s2 = "啦"; String s3 = "哈哈"; String s4 = "歡樂"; String s5 = "嘿嘿"; System.out.println(s1.contains(s2)); //判斷是否包含傳入的字符串 System.out.println(s1.contains(s3)); System.out.println("--------------------------------"); System.out.println(s1.startsWith(s4)); System.out.println(s1.endsWith(s5)); } private static void demo1() { String s1 = "allidea"; String s2 = "allidea"; String s3 = "Allidea"; System.out.println(s1.equals(s2)); System.out.println(s2.equals(s3)); System.out.println("----------------------"); System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); //不區分大小寫 }
A:案例演示
public class String_test_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i ++) { System.out.println("請輸入用戶名:"); String userName = sc.nextLine(); System.out.println("請輸入密碼"); String password = sc.nextLine(); //若是是字符串常量和字符串變量比較,都是字符串常量調用方法,將變量看成參數傳遞,防止空指針異常 if ("admin".equals(userName) && "123456".equals(password)) { System.out.println("歡迎" + userName + "登陸"); break; //跳出循環 } else { if (i == 2) { System.out.println("您的次數已到,請明天再來吧"); } System.out.println("輸入錯誤,您還有" + (2 - i) + "次機會,請從新輸入。"); } } } }
A:String類的獲取功能
public class String_test_2 { public static void main(String[] args) { // demo1(); // demo2(); // demo3(); String s1 = "allidea"; String s2 = s1.substring(3); System.out.println(s2); String s3 = s1.substring(0, 2); //包含頭,不包含尾,左閉右開 System.out.println(s3); } private static void demo3() { String s1 = "allidea.net"; int index1 = s1.indexOf("i",2); //從指定位置向後找 System.out.println(index1); int index2 = s1.indexOf("i"); //從後向前找,第一次出現的字符 System.out.println(index2); int index3 = s1.lastIndexOf('a',7); System.out.println(index3); } private static void demo2() { String s1 = "allidea"; int index = s1.indexOf('a'); //參數接收的是int類型的,傳遞char類型會自動提高。 System.out.println(index); int index2 = s1.indexOf('f'); //若是不存在返回的是-1 System.out.println(index2); int index3 = s1.indexOf("ll"); //獲取字符串中第一個字符出現的位置 System.out.println(index3); int index4 = s1.indexOf("lf"); //若是不存在返回的是-1 System.out.println(index4); } private static void demo1() { // int[] arr = {11,22,33}; // System.out.println(arr.length); //數組中的length是屬性 String s1 = "allidea"; System.out.println(s1.length()); //length()是一個方法,獲取的是每個字符的個數。 String s2 = "你吃了嗎?"; System.out.println(s2.length()); char c = s2.charAt(3); //根據索引獲取對應位置的字符 System.out.println(c); char c2 = s2.charAt(8); System.out.println(c2); //StringIndexOutOfBoundsException字符串索引越界異常 } }
A:案例演示
public class String_test_2 { public static void main(String[] args) { String s = "allidea"; for (int i = 0; i < s.length(); i++) { //經過for循環得到自字符串的索引 // char c = s.charAt(i); // System.out.print(c);s System.out.print(s.charAt(i)); //經過索引獲取每個字符 } } }
A:案例演示
public class String_test_3 { // 分析:字符串都是由字符組成的,而字符的值都是有範圍的,經過範圍來判斷是否包括該字符 // 若是包含就讓計數器變量自增 public static void main(String[] args) { String s = "ABCDEabcd123456!@#$%^*/"; int big = 0; int small = 0; int num = 0; int other = 0; //1.獲取每個字符,for循環遍歷 for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); //經過索引獲取每個字符 //2.判斷字符是否在這個範圍內 if (c >= 'A' && c <= 'Z') { big++; //若是知足大寫字母,就讓其對應的變量自增 } else if (c >= 'a' && c <= 'z') { small++; //若是知足小寫字母,就讓其對應的變量自增 } else if (c >= '0' && c <= '9') { num++; //若是知足數字,就讓其對應的變量自增 } else { other++; //若是知足大寫字母,就讓其對應的變量自增 } } //打印每個計數器的結果 System.out.println(s + "中的大寫字母有:" + big + "個,小寫字母有:" + small + "個,數字有:" + num + "個,特殊符號有:" + other + "個。"); } }
A:String的轉換功能:
static String valueOf(int i):把int類型的數據轉成字符串。
import net.allidea.bean.Person; public class String_6 { public static void main(String[] args) { // demo1(); // demo2(); // demo3(); String s1 = "ALLIDEA"; String s2 = "chengxuyuan"; String s3 = s1.toLowerCase(); String s4 = s2.toUpperCase(); System.out.println(s3); System.out.println(s4); System.out.println(s3 + s4);//用+拼接字符串更強大,能夠用字符串與任意類型相加 System.out.println(s3.concat(s4)); } private static void demo3() { char[] arr = {'a','b','c'}; String s = String.valueOf(arr);//底層是由String類的構造方法完成的,把字符數組轉成字符串 System.out.println(s); String s2 = String.valueOf(100);//將100轉換爲字符串 System.out.println(s2); Person p1 = new Person("張三",23); System.out.println(p1); String s3 = String.valueOf(p1); //調用的是對象的toString方法 System.out.println(s3); } private static void demo2() { String s = "allidea"; char[] arr = s.toCharArray(); //將字符串轉換爲字符數組 for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } private static void demo1() { String s1 = "abc"; byte[] arr = s1.getBytes(); for (int i = 0; i < arr.length; i++) { // System.out.print(arr[i] + ""); } String s2 = "你好你好"; byte[] arr2 = s2.getBytes(); //經過gbk碼錶將字符串轉換成字節數組,一箇中文表明兩個字節 for (int i = 0; i < arr2.length; i++) {//gbk碼錶特色,中文的第一個字節確定是負數 // System.out.print(arr2[i] + ""); } String s3 = "琲"; byte[] arr3 = s3.getBytes(); for (int i = 0; i < arr3.length; i++) { System.out.print(arr3[i] + ""); } } }
A:案例演示
public class String_test_4 { public static void main(String[] args) { String s = "allidea"; String s2 = s.substring(0, 1 ).toUpperCase().concat(s.substring(1).toLowerCase()); System.out.println(s2); } }
A:案例演示
需求:把數組中的數據按照指定個格式拼接成一個字符串
舉例:
輸出結果:
public class String_test_5 { /* * 分析: * 1.須要定義一個字符串 * 2.遍歷數組獲取每個元素 * 3.用字符串與數組中的元素進行拼接 * */ public static void main(String[] args) { int[] arr = {1,2,3}; String s = "["; for (int i = 0; i < arr.length; i++) { if (i == arr.length - 1) { s = s + arr[i] + "]"; } else { s = s + arr[i] + ", "; } } System.out.println(s); } }
A:String的替換功能及案例演示
B:String的去除字符串兩空格及案例演示
C:String的按字典順序比較兩個字符串及案例演示
public class String_test_6 { public static void main(String[] args) { // demo1(); // demo2(); String s1 = "abc"; String s2 = "de"; int num = s1.compareTo(s2); //按照碼錶值比較 System.out.println(num); String s3 = "鄭"; String s4 = "成"; int num2 = s3.compareTo(s4); System.out.println('鄭' + 0); //查找的是Unicode碼錶值 System.out.println('成' + 0); System.out.println(num2); String s5 = "nihao"; String s6 = "NIHAO"; int num3 = s5.compareTo(s6); System.out.println(num3); int num4 = s5.compareToIgnoreCase(s6); System.out.println(num4); } private static void demo2() { String s = " all ide a "; String s2 = s.trim(); System.out.println(s2); } private static void demo1() { String s = "allidea"; String s2 = s.replace('i', 'I');//I替換i,若不存在,則不變 System.out.println(s2); String s3 = s.replace("id", "ID"); System.out.println(s3); } }
A:案例演示
需求:把字符串反轉
B:分析
import java.util.Scanner; public class String_test_7 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串:"); String line = sc.nextLine(); //將鍵盤錄入的字符串存儲在line中 char[] arr = line.toCharArray(); //將字符串轉換成數組 String s = ""; for (int i = arr.length - 1; i >= 0; i--) { //倒着遍歷數組 s = s + arr[i]; //拼接成字符串 } System.out.println(s); } }
A:思路圖解
B:代碼實現-案例演示
public class String_test_8 { /* 分析 1.定義計數器變量,變量爲0 2.經過indexOf方法在大串中找小串 若是沒有返回-1程序結束 若是有則返回索引值 3.根據獲取的索引值,加上小串的長度,截取大串,將截取後的結果賦值給大串。 4.回到第二部,繼續 5.返回-1,程序結束。 */ public static void main(String[] args) { String max = "nizhidaoma,wozhingshiyaochengweiyidaijavadashendenanren,zhidaoma,zhi.";//定義大串 String min = "zhi"; //定義小串 int count = 0; //定義計數器變量 int index = 0; //定義索引 while ((index = max.indexOf(min)) != -1) { //定義循環,判斷小串是否在大串中出現 count++; //計數器自增 max = max.substring(index + min.length()); } System.out.println(count); } }
import java.util.Scanner; public class Other_11_test { public static void main(String[] args) { login(); } private static void login() { Scanner sc = new Scanner(System.in); System.out.println("請輸入用戶名:"); for (int i = 0; i < 6; i++) { String s = sc.nextLine(); if (checkNum(s)) { System.out.println("驗證成功!"); break; } else { if (i == 5) { System.out.println("您的次數已到,請明天再來吧"); break; } System.out.println("您還有" + (5 - i) + "次機會,請從新輸入。"); } } } public static boolean checkNum(String str) { String msg = ""; boolean flag = true; if (str.isEmpty()) { msg += "不能爲空,"; flag = false; } if (str.length() <= 6) { msg += "長度不能少於6,"; flag = false; } if (getNum(str) && str.length() <= 6) { msg += "且"; flag = false; } if (getNum(str)) { msg += "不能含有數字,"; flag = false; } if (flag == false) { System.out.print(msg); } return flag; } private static boolean getNum(String s) { final String number = "0123456789"; for (int j = 0; j < s.length(); j++) { if (number.indexOf(s.charAt(j)) != -1) { return true; } } return false; } }