生成新的数字组合字母数字

时间:2015-03-11 08:28:10

标签: java algorithm numbers

我想创建一种方法,以便将来为数据库记录生成编号。

我的规则是这样的:

[start] - [end]

0001-
9999

A001-
A999

AA01-
AA99

AB01-
AB99

AC01-
AC99

etc...

......

......

ZZZZ

使用Excel列编号看起来相似。 如何使用Java创建这样的东西?

这是我的代码: 但我混淆了如何检查最后一个数字,如9999,A999等

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;

    }

请帮忙。谢谢。

2 个答案:

答案 0 :(得分:0)

看看这个片段。您必须根据基数36转换数字。

int[] ints = { 0, 1, 10, 35, 36, 46, 36*36-1, 36*36*36-1, 36*36*36*36-1};
for (int i : ints) {
    System.out.printf("int: %7d   string: %4s%n", i, Integer.toString(i, 36));
}

<强>输出

int:       0   string:    0
int:       1   string:    1
int:      10   string:    a
int:      35   string:    z
int:      36   string:   10
int:      46   string:   1a
int:    1295   string:   zz
int:   46655   string:  zzz
int: 1679615   string: zzzz

答案 1 :(得分:0)

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;
}

这应该有效。或者至少你明白这个想法..;)