如何从十个同学的身份证中提取每个同学的生日?(语言-java)

用户自己输入十个同学的身份证,并提取每个同学的生日,并且统计每个月有多少同学生日。用Java中的substring来提取生日。 不可以使用map来统计月份 ,因为本人还没有学到这个知识点。最好把所有的代码写出来,就是这道题目要把我搞死了

1)代码有详细的注释,给你直接上代码,用的是substring
2)输入数据,程序运行后,直接copy粘贴后,回车,就有结果了
431024199702097816
431024199701097816
431024199703097816
431024199703057816
431024199704017816
q
2)运行结果图

img

img

3)实现代码



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author pcj
 * @date 2022/5/9
 * @note https://ask.csdn.net/questions/7712769
 */
public class T5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> identityIds = new ArrayList<String>();
        //身份证号码验证
        String regex = "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        Pattern pattern =Pattern.compile(regex);

        //操作提示语
        System.out.println("操作提示:回车结束一个身份证输入");
        System.out.println("操作提示:q结束输入");

        //接受输入
        while (true){
            String identityId = scanner.next();
            if (identityId.equals("q")){
                System.out.println("当前已输入" + identityIds.size() + "个身份证");
                System.out.println("输入结束,开始统计。。。");
                break;
            } else {
                Matcher matcher = pattern.matcher(identityId);
                if (matcher.find()) {
                    identityIds.add(identityId);
                } else {
                    System.out.println("格式不对,请重新输入");
                }
            }
        }

        Integer month01count=0, month02count=0,month03count=0,month04count=0, month05count=0,month06count=0,
                month07count=0, month08count=0,month09count=0,month10count=0, month11count=0,month12count=0;
        //开始统计
        for (String identityId : identityIds) {
            String  birthday= identityId.substring(6, 14);
            Integer monthint = Integer.parseInt(birthday.substring(4,6));
            switch (monthint){
                case 1:
                    month01count++;
                    break;
                case 2:
                    month02count++;
                    break;
                case 3:
                    month03count++;
                    break;
                case 4:
                    month04count++;
                    break;
                case 5:
                    month05count++;
                    break;
                case 6:
                    month06count++;
                    break;
                case 7:
                    month07count++;
                    break;
                case 8:
                    month08count++;
                    break;
                case 9:
                    month09count++;
                    break;
                case 10:
                    month10count++;
                    break;
                case 11:
                    month11count++;
                    break;
                case 12:
                    month12count++;
                    break;
            }
        }

        System.out.println("1月生日的同学有:" + month01count + " 个");
        System.out.println("2月生日的同学有:" + month02count + " 个");
        System.out.println("3月生日的同学有:" + month03count + " 个");
        System.out.println("4月生日的同学有:" + month04count + " 个");
        System.out.println("5月生日的同学有:" + month05count + " 个");
        System.out.println("6月生日的同学有:" + month06count + " 个");
        System.out.println("7月生日的同学有:" + month07count + " 个");
        System.out.println("8月生日的同学有:" + month08count + " 个");
        System.out.println("9月生日的同学有:" + month09count + " 个");
        System.out.println("10月生日的同学有:" + month10count + " 个");
        System.out.println("11月生日的同学有:" + month11count + " 个");
        System.out.println("12月生日的同学有:" + month12count + " 个");
        System.out.println("统计结束");
    }
}


package com.example;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        String regex = "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        Scanner scanner = new Scanner(System.in);
        Pattern pattern = Pattern.compile(regex);
        Map<String, List<String>> birthdayMap = new HashMap<>();
        for (int i=0; i<10; ) {
            String birthdayStr = scanner.next();
            Matcher matcher = pattern.matcher(birthdayStr);
            if (matcher.find()) {
                String birthday = birthdayStr.substring(6, 14);
                String month = birthdayStr.substring(10, 12);
                List<String> monthBirthdays = new ArrayList<>();
                if (birthdayMap.containsKey(month)) {
                    monthBirthdays = birthdayMap.get(month);
                }
                monthBirthdays.add(birthday);
                birthdayMap.put(month, monthBirthdays);
                i++;
            } else {
                System.out.println("身份证格式不正确,请重新输入。");
            }
        }

        // 输出同学生日
        birthdayMap.values().stream().flatMap(Collection::stream)
                .forEach(birthday -> System.out.println(birthday));

        // 统计每个月有多少同学生日
        birthdayMap.entrySet().stream()
                .forEach(entry -> System.out.println(entry.getKey() + "月有" + entry.getValue().size() + "个同学生日"));

    }
}

身份证号直接按照字符串string输入保存,因为年月日是固定的位置,**20020823下标由0开始的,java调用函数截取
10--13代表月和日,把字段保存,在做判断月份,,每个月份的个数

给你提供个思路
准备一个检验身份证长度是否合法的函数,不合法重新输入
合法就从下标6的位置截取6-14位,这就提取完了
声明一个学生对象,包括身份证号,姓名
从第二步得到的生日里循环每一条记录,每条记录截取4-6位,放入list
声明一个map,键是月份,值是次数,有12个值,循环统计次数

思路:将输入身份证字符串转化为字符,使用substr()函数或subString()函数截取第七位至十四位生日,再截取对应年月日保存到本地变量。之后遍历月份,出现则月份同学数量加一,实现统计

思路:用字符串对应位置提取就行,用map存储月份


import java.util.*;
public class Solution {

    public static void main(String[] args) {
        String[]ids = new String[10];
        Map<Integer,Integer> monthBirth = new HashMap<>();
        try(Scanner scanner = new Scanner(System.in)){
            for(int i=0;i<10;i++){
                ids[i] = scanner.nextLine().trim();
                int birthDayMonth = Integer.parseInt(ids[i].substring(6+4,6+6));
                monthBirth.put(birthDayMonth,
                        monthBirth.getOrDefault(birthDayMonth,0)+1);
            }
            for(int i=1;i<=12;i++){
                System.out.printf("%d月 有%d人过生日\n", i, monthBirth.getOrDefault(i,0));
            }
        }
    }
}
/*
111111199909032222
111111196901042222
111111200002152222
111111201912232222
111111198905132222
111111197907042222
111111200408172222
111111200203082222
111111200103062222
111111199711052222
* */

img

1.定义一个集合存储所有输入的身份证号码;
2.循环输入所有的身份证号码存到集合中;
3.把集合 按截取substring(10,12)来分组,统计数量;
给出第三步的代码:


list.stream().collect(Collectors.groupingBy(v -> Integer.valueOf(v.substring(10, 12)), Collectors.counting())).forEach((k,v) -> {
            System.out.println(String.format("%d 月有 %d 人过生日",k,v));
        });