生成新的数字组合字母数字 [英] Generate new number combination alphanumeric
本文介绍了生成新的数字组合字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一种在将来为数据库记录生成编号的方法.
I want to create method thath generate numbering for Database record in the future.
我的规则是这样的:
[start]
-[end]
0001-
9999
A001-
A999
AA01-
AA99
AB01-
AB99
AC01-
AC99
etc...
......
......
ZZZZ
使用Excel列编号看起来很相似. 如何使用Java创建类似的代码?
Its look similar using Excel column numbering. How to create like that, using Java?
这是我的代码: 但是我在如何检查最后一个数字(例如9999,A999等)中感到困惑
Here is my code : But i confuse in how to check if in the last number like 9999 , A999 etc
public static void main(String [] args) {
String lastSchCode = "9999";
System.out.println(generateSchCode(lastSchCode));
}
public static String generateNextNum(String number) {
int nextNum = Integer.parseInt(number);
String padNextNum = lPadZero(nextNum+1, 4);
return padNextNum;
}
public static String generateSchCode(String lastSchCode) {
String nextSchCode = null;
String [] alphabets = {"A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter = 0;
for (int i = 0; i < lastSchCode.length(); i++) {
if (lastSchCode.charAt(i) == '9') {
counter++;
}
}
if (generateNextNum(lastSchCode).equals("10000")) {
int num = 9999;
} else {
}
return nextSchCode;
}
请帮助.谢谢.
推荐答案
public static void main(String [] args) {
String lastSchCode = "9999";
System.out.println(generateSchCode(lastSchCode));
}
public static String generateNextNum(String number) {
int nextNum = Integer.parseInt(number);
String padNextNum = lPadZero(nextNum+1, 4);
return padNextNum;
}
public static String generateSchCode(String lastSchCode) {
String nextSchCode = null;
String [] alphabets = {"0","1","2","3","4","5","6","7","8","9","A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
List<String> alphabetsAsList = Arrays.asList(alphabets);
int counter = 0;
for (int i = lastSchCode.length(); i > 0; --i) {
if(lastSchCode.charAt(i) == '9'){
incrementWith9(lastSchCode,i);
}
else{
String s = lastSchCode.substring(index,index+1);
String incrementedString = alphabetsAsList.get(alphabetsAsList.indexOf(s) + 1);
char[] charArr = lastSchCode.toCharArray();
charArr[index] = incrementedString.charAt(0);
nextSchCode = charArr.toString();
}
}
return nextSchCode ;
}
public String incrementWith9(String input, int index){
char[] ca = input.toCharArray();
ca[index] = '0';
if( index != 0 && input.charAt(index -1 ) == '9'){
incrementWith9(ca.toString(),index -1);
}
elseif(index == 0 ){
return "A000";
}
else{
String [] alphabets = {"0","1","2","3","4","5","6","7","8","9","A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
List<String> alphabetsAsList = Arrays.asList(alphabets);
String s = input.substring(i,i+1);
String incrementedString = alphabetsAsList.get(alphabetsAsList.indexOf(s) + 1);
char[] charArr = input.toCharArray();
charArr[i] = incrementedString.charAt(0);
nextSchCode = charArr.toString();
}
return nextSchCode;
}
这应该有效.或者,至少您对这个想法是正确的..;)
This should work. Or atleast you get the idea right.. ;)
这篇关于生成新的数字组合字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文