Java大写字母不重复输出_java生成4位不重复字符(包含大写字母、小写字母、数字)...

需要生成4位不重复的字符作为唯一参数,要求只能使用大写字母、小写字母和数字的组合。大写字母26个、小写字母26个、10个数字共62个字符。不重复排列个数:62*62*62*62=14776336个。

/**

*

* @author wzp

* 2016-3-18

*/

public class DmSequenceUtil {

static String bwords[] = { "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", "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" };

static Random random = new Random();

static int index1 = 0;

static int index2 = 0;

static int index3 = 0;

static int index4 = 0;

static boolean isRead = true;

/**

* 支持14776336个不重复4位字符串 2016-3-18

*

* @return

* @throws Exception

*/

public static synchronized String getNoRepeatId() throws Exception {

// 判断是否需要读取文件记录

if (isRead) {

String sequence = read();

if (sequence != null && sequence.length() == 4) {

index1 = getIndex(String.valueOf(sequence.charAt(0))) + 10;

index2 = getIndex(String.valueOf(sequence.charAt(1))) + 10;

index3 = getIndex(String.valueOf(sequence.charAt(2))) + 10;

index4 = getIndex(String.valueOf(sequence.charAt(3))) + 10;

}

isRead = false;

}

getRandom();

String id = "" + bwords[index1 == 0 ? 0 : index1 - 1]

+ bwords[index2 == 0 ? 0 : index2 - 1]

+ bwords[index3 == 0 ? 0 : index3 - 1] + bwords[index4 - 1];

write(id);

return id;

}

private static int getIndex(String word) {

for (int i = 0; i < bwords.length; i++) {

if (bwords[i].equals(word)) {

return i;

}

}

return 0;

}

private static void getRandom() throws Exception {

if (index4 < 62) {

index4++;

} else {

index4 = 1;

if (index3 < 62) {

index3++;

} else {

index3 = 1;

if (index2 < 62) {

index2++;

} else {

index2 = 1;

if (index1 < 62) {

index1++;

} else {

throw new Exception("结束");

}

}

}

}

}

private static void write(String sequence) throws IOException {

File file = new File("sequence.dat");

FileWriter fw = new FileWriter(file, false);

fw.write(sequence);

fw.flush();

fw.close();

}

private static String read() throws IOException {

String sequence = "";

File file = new File("sequence.dat");

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

sequence = br.readLine();

br.close();

fr.close();

} catch (Exception e) {

System.err.println("读取文件出错");

}

return sequence;

}

}

Java高级群1群:224651178

Java高级群1群:134787504

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值