Generate new number combination alphanumeric

旧时模样 提交于 2019-12-13 09:42:02

问题


I want to create method thath generate numbering for Database record in the future.

My rules like this :

[start] - [end]

0001-
9999

A001-
A999

AA01-
AA99

AB01-
AB99

AC01-
AC99

etc...

......

......

ZZZZ

Its look similar using Excel column numbering. How to create like that, using Java?

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;

    }

Please help. Thank you.


回答1:


Have a look at this snippet. You have to convert the number based on radix 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));
}

output

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



回答2:


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.. ;)



来源:https://stackoverflow.com/questions/28981669/generate-new-number-combination-alphanumeric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!