码迷,mamicode.com
首页 > 编程语言 > 详细

java中获取字母和数字的组合

时间:2017-12-13 17:20:45      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ext   str   muti   版权   util   出现   pack   har   tostring   

  

/**
 * AlgorithmUtil.java
 * com.tfedu.yuwen.util
 * Copyright (c) 2017, 北京聚智未来科技有限公司版权所有.
*/

package com.tfedu.yuwen.util;

import java.util.Random;

/**
 * 算法工具类
 * <p>
 *  语文真好中各种算法
 * @author   kuangxiang(kuangxiang666@yeah.net)
 * @Date     2017年9月18日      
 */
public class AlgorithmUtil {

    /**
     * 位数
     * <P>
     * 设置生成字符串的位数
     */
    private static final int DIGIT = 8;

    /**
     * 验证码位数
     */
    private static final int VERIFYCODEDIGIT = 6;

    /**
     * 
     * 生成圈ID
     * <P>
     * 8位大写字母和数字的组合, 两者不必同时出现,但字符能重复出现, 但是必须是唯一的
     *
     * @return 字符串
     */
    public static String generateSign() {
        int i;
        int count = 0;
        char[] str = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘,
                ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ };
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < DIGIT) {
            i = Math.abs(r.nextInt(str.length));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }

    /**
     * 生成验证码
     *
     * @return 验证码字符窜
     */
    public static String generateVerifyCode() {
        int i;
        int count = 0;
        char[] str = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ };
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < VERIFYCODEDIGIT) {
            i = Math.abs(r.nextInt(str.length));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }
}

 

java中获取字母和数字的组合

标签:ext   str   muti   版权   util   出现   pack   har   tostring   

原文地址:http://www.cnblogs.com/htyj/p/8032950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!