第十三章作业
1.将字符串指定部分进行反转
-
字符翻转 ,如何实现
-
逻辑判断,选出正确的条件,然后取反即可。
-
抛出异常,然后使用 try catch处理
package com.homework13; /** * @author whj * @version 1.0 */ public class Homework01 { public static void main(String[] args) { String str = "abcdefghijklmn"; try{ str = reverse(str, 1, 5); } catch(Exception e){ System.out.println(e.getMessage()); return; } System.out.println(str); } public static String reverse(String str,int start,int end){ if (!(str != null && start >= 0 && end > start && end < str.length())){ throw new RuntimeException("参数不正确"); } char[] chars = str.toCharArray(); char temp = ' '; for (int i = start, j = end; i < j; i++,j--) { temp = chars[j]; chars[j] = chars[i]; chars[i] = temp; } return new String(chars); } }
2.用户注册
-
使用到的函数 str.indexof(’ ');//得到字符的位置
-
对用户信息进行逻辑判断,选出正确的条件取反即可。
-
字符串是否都是数字,将字符串转成数组,再判断每一项是否都在’0‘ - '9’之间即可;
-
不满足条件,抛出异常即可
package com.homework13; import java.util.Scanner; /** * @author whj * @version 1.0 */ public class Homework02 { public static void main(String[] args) { Person person = new Person(); String name = "jack"; String pwd = "123456"; String email = "123@ .343"; try { person.userRegister(name,pwd,email); System.out.println("注册成功"); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("注册失败"); } System.out.println(person.toString()); } } class Person{ private String name = " "; private String pwd = " "; private String email = " "; public Person() { } public Person(String name, String pwd, String email) { setName(name); setPwd(pwd); setEmail(email); } public String getName() { return name; } public void setName(String name) { if (name.length() > 4 || name.length() < 2){ // System.out.println("姓名长度输入错误(2-4之间)"); throw new RuntimeException("姓名长度输入错误(2-4之间)"); } this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { if (pwd.length()!=6){ // System.out.println("密码长度有误!!"); throw new RuntimeException("密码长度有误!!"); } char chars[] = pwd.toCharArray(); for (int i = 0; i < chars.length; i++) { if (chars[i] > '9' || chars[i] < '0'){ // System.out.println("密码只能是数字"); throw new RuntimeException("密码只能是数字"); } } this.pwd = pwd; } public String getEmail() { return email; } public void setEmail(String email) { int index1 = email.indexOf('@'); int index2 = email.indexOf('.'); if (!(index1 != -1 && index2 !=-1 && index1 == index2 -1)){ System.out.println("格式错误"); throw new RuntimeException("邮箱格式错误"); } this.email = email; } public void userRegister(String name, String pwd, String email){ setName(name); setPwd(pwd); setEmail(email); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", pwd='" + pwd + '\'' + ", email='" + email + '\'' + '}'; } }
3.按照格式输出名字信息
-
函数:str.substring(start,end);//得到子字符串
-
split(" ");//按照空格分隔,老师用的这种,得到字符串数组
-
String.format()函数,按照一定格式转换得到字符串
package com.homework13;
/**
* @author whj
* @version 1.0
*/
public class Homework03 {
public static void main(String[] args) {
String name = "Hancvv ghunff Pfssing";
printName(name);
}
// public static void printName(String name){//简陋版本
// int index1 = name.indexOf(' ');
// int index2 = name.lastIndexOf(' ');
// String str1 = name.substring(0,index1);
// String str2 = name.substring(index1 + 1, index2 - 1);
// String str3 = name.substring(index2 + 1);
// System.out.println(str3 + "," + str1 + "." + str2.substring(0,1).toUpperCase());
// }
public static void printName(String name){//简陋版本
if (name == null){
System.out.println("name 不能为空。");
return;
}
String[] str = name.split(" ");
if (str.length != 3){
System.out.println("格式不正确。");
return;
}
String format = String.format("%s,%s.%c", str[2], str[0], str[1].toUpperCase().charAt(0));
System.out.println(format);
}
}
4.判断字符类型
-
charAt函数
-
toCharArray()函数
package com.homework13; /** * @author whj * @version 1.0 */ public class Homework04 { public static void main(String[] args) { count("ASAhha 11H"); } public static void count(String str){ if (str == null){ System.out.println("不能为空。"); return; } int counterNum = 0; int counterUp = 0; int counterLow = 0; int counterOther = 0 char[] chars = str.toCharArray();//或是通过charAt()得到对应的字符 for(int j = 0; j < chars.length; j++) { if (chars[j] >= '0' && chars[j] <= '9'){ counterNum++; }else if (chars[j] >= 'A' && chars[j] <= 'Z'){ counterUp++; }else if (chars[j] >= 'a' && chars[j] <= 'z'){ counterLow++; } else{ counterOther++; } } System.out.println("数字:" + counterNum + "\n大写字母:" + counterUp + "\n小写字母:" + counterLow); } }
5.String内存分布
- 1.直接赋值,指向常量池;
- 2.创建字符串对象,对象中的value指向常量池,若常量池中存在对应的字符串,直接指向该字符串;