String&Array
- 练习
- 关于String类的方法和Array的操作
- 字符串分割,数组排序,字符串赋值给自定义对象并添加到数组中
题目:
-
从键盘上输入身份证号, 判断出生日期,性别,
身份证倒数第二位表示性别,双数为女性,单为男 -
有字符串: ”101,lisi,98;
202,wangwu,76;
303,chenqi,84;
404,zhangsan,49;
505,xiaoming,67”,
保存的学生的学号,姓名,成绩信息.
要求把字符串中学生信息取出来,创建Student对象,
把Student对象保存到数组中;遍历学生对象数组;
在数组中查找名字为xiaoxiao同学是否存在.对数组中学生对象根据成绩降序排序
import java.util.Scanner;
/**
* @ClassName Judge
* @Deacription TODO 1. 从键盘上输入身份证号, 判断出生日期,性别
* 2. 身份证倒数第二位表示性别,双数为女性,单为男
* @Version 1.0
*/
public class Judge {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean verify = true;
while (verify){
System.out.print("请输入你的身份证号码:");
String id = sc.next();
// 用正则表达验证控制台输入
boolean control = id.matches("\\d{17}(\\d|X|x)");
if (control){
// 截取身份证中出生日期
String birth = id.substring(6, 14);
// 取出倒数第二位的元素,偶数为女性
char c = id.charAt(id.length() - 2);
if (c % 2 == 0){
System.out.println("女性\t" + "出生日期:" + birth);
// 判断成功结束
verify = false;
}else {
System.out.println("男性\t" + "出生日期:" + birth);
verify = false;
}
}else{
//不合法继续循环
System.out.println("身份证输入不合法...请重新输入!");
}
}
}
}
Student类
public class Student implements Comparable<Student>{
private String id;
private String name;
private String score;
public Student(String id, String name, String score) {
this.id = id;
this.name = name;
this.score = score;
}
public Student() {
}
public String getId() {
return id;
}
public Student setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Student setName(String name) {
this.name = name;
return this;
}
public String getScore() {
return score;
}
public Student setScore(String score) {
this.score = score;
return this;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", score='" + score + '\'' +
'}';
}
@Override
public int compareTo(Student o) {
return o.getScore().compareTo(this.getScore());
}
}
测试类
/**
* @ClassName StudentTest
* @Deacription
* TODO
* 有字符串: ”101,lisi,98;
* 202,wangwu,76;
* 303,chenqi,84;
* 404,zhangsan,49;
* 505,xiaoming,67”,
* 保存的学生的学号,姓名,成绩信息.
* 要求把字符串中学生信息取出来,创建Student对象,
* 把Student对象保存到数组中;遍历学生对象数组;
* 在数组中查找名字为xiaoxiao同学是否存在.对数组中学生对象根据成绩降序排序
* @Version 1.0
*/
public class StudentTest {
public static void main(String[] args) {
String src = "101,lisi,98;202,wangwu,76;303,chenqi,84;404,zhangsan,49;505,xiaoming,67";
// 分号替换成逗号
String replace = src.replace(";", ",");
// 通过逗号分隔字符串
String[] split = replace.split(",");
for (String s : split) {
System.out.print(s + " ");
}
//创建学生数组
Student[] stus = new Student[5];
// 思路,内循环,分隔字符串后并按照每3个字符分别赋值给不同的对象
// split[i]元素下标为持续+1,定义初始值,循环自增
int index = 0;
for (int i = 0; i < stus.length ; i++) {
String id = split[index++];
String name = split[index++];
String score = split[index++];
Student student = new Student(id,name,score);
stus[i] = student;
}
System.out.println();
// 遍历学生对象,未排序
for (Student student : stus) {
System.out.println(student);
}
// 对自定义类型进行排序需要实现Comparable并实现CompareTo方法重写规则
// 否则报错Exception in thread "main" java.lang.ClassCastException
Arrays.sort(stus); // sort默认升序,已在Student类重新编写排序规则
//判断学生是否存在
if (isExist1(stus,"xiaoxiao")){
System.out.println("学生存在");
}else{
System.out.println("不存在");
}
// 按照成绩降序输出
System.out.println("按照学生成绩降序输出");
for (Student student : stus) {
System.out.println(student);
}
}
public static boolean isExist1(Student[] stus ,String target){
for (int i = 0; i < stus.length; i++) {
if (target.equals(stus[i].getName())){
return true;
}
}
return false;
}
}