java常用工具类

java常用工具类

Scroll Down

统计段落中出现某一个词的次数

    public static void wordCount(){
        Scanner scanner = new Scanner(System.in);
        int count = 0,index = 0;
        System.out.println("请输入句子:");
        String s = scanner.nextLine();
        System.out.println("请输入要查询的词:");
        String card = scanner.next();
        while ((index = s.indexOf(card,index)) != -1){
            index = index + card.length();
            count++;
        }
        System.out.println(card + "在句子中出现了"+ count + "次");
    }

一个随机字符串,判断有多少字母有多少数字

public static void letterAndnum(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入随机字符串:");
    String s = scanner.nextLine();
    int length = s.length();
    int num = 0,let = 0;
    for (int i = 0; i < length; i++){
        if (Character.isDigit(s.charAt(i))){
            num++;
        }
        if (Character.isLetter(s.charAt(i))){
            let++;
        }
    }
    System.out.println("数字:"+num+"   字母:"+let);
}

根据特定日期格式拆分日期

    //如:请输入一个日期(格式如:**月**日****年)经过处理得到: ****年**月**日
    public static void splitDate(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入日期:(格式如:**月**日****年)");
        String s = scanner.nextLine();
        int i = s.indexOf("日");
        String substring = s.substring(i + 1);
        String substring1 = s.substring(0, i + 1);
        System.out.println(substring + substring1);
    }

敏感词过滤程序

public static void fitterWords(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入聊天内容:");
    String str = scanner.nextLine();
    String ch[] = {"色情","爆炸","恐怖","军火"};
    for (String s:ch) {
        str = str.replaceAll(s,"*");
    }
    System.out.println("聊天内容:"+str);
}

计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数

public static void leadTime() throws ParseException {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入第一个日期:");
    String str = scanner.nextLine();
    System.out.println("请输入第二个日期:");
    String str2 = scanner.nextLine();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date parse = dateFormat.parse(str);
    Date parse1 = dateFormat.parse(str2);
    long a = 0;
    if (parse.after(parse1)){
        a = parse.getTime() - parse1.getTime();
    }else {
        a = parse1.getTime() - parse.getTime();
    }
    long oneDay = 1000*60*60*24;
    long day = a/oneDay;
    long week = day/7;
    System.out.println("相隔:"+day+" 天");
    System.out.println("相隔:"+week+" 周");
}

设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出

public static void letterUpperLower(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入字符串:");
    String str = scanner.nextLine();
    char[] chars = str.toCharArray();
    System.out.println("大写字母为:");
    for (char a:chars) {
        if (Character.isUpperCase(a)){
            System.out.println(a);
        }
    }
    System.out.println("小写字母为:");
    for (char a:chars) {
        if (Character.isLowerCase(a)){
            System.out.println(a);
        }
    }
}

计算并输出21世纪的闰年,程序的执行时间

public static void leapYeay21(){
    long start = System.currentTimeMillis();
    GregorianCalendar calendar = new GregorianCalendar();
    for (int i = 2000;i<2100;i++){
        if (calendar.isLeapYear(i)){
            System.out.println(i + "是闰年");
        }
    }
    long end = System.currentTimeMillis();
    System.out.println("程序的执行时间为:"+(end - start)+"毫秒");
}

当以yyyy-MM-dd格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几

public static void isLeapYeay() throws ParseException {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入日期:(格式:yyyy-MM-dd)");
    String str = scanner.nextLine();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date parse = dateFormat.parse(str);
    Calendar instance = Calendar.getInstance();
    instance.setTime(parse);
    int year = instance.get(Calendar.YEAR);
    int month = instance.get(Calendar.MONTH)+1;
    int week = instance.get(Calendar.DAY_OF_WEEK)-1;
    GregorianCalendar calendar = new GregorianCalendar();
    if (calendar.isLeapYear(year)){
        System.out.println(year + "是闰年");
    }else{
        System.out.println(year + "是平年");
    }
    int max = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
    String s = week == 0?"周日":"周"+week;
    System.out.println(month + "月有" + max + "日,该日是" + s);
}