Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos

Applies To : JDK 1.5 / JDK 1.6

Summary :

In many custom application, we might required to generate unique alpha numeric number. In this document I am discussing step by step guide with java code.

Author : Biplab Ray

Company : TATA Consultancy Services

Created on : 31st January 2015

Author Bio :

Biplab Ray is working for Tata Consultancy Service as Assistant Consultant and development of SAP EP , composite applications using CAF, BPM, BRM, WebDynpro for Java. He also works in technologies like Java/J2EE and has depth understanding on eSOA, Webservice, Enterprise Service, XML.

Introduction

In many custom application, we might required to generate the unique alpha numeric number. It might have below conditions to generate the numbers.

a) The length of the number should not be restricted.

b) All the inputs should be variable and configurable.

c) The number range should have upper and lower limit.

d) Should have some special conditions, at which position character of the number would be numeric or alpha.

e) Application should have conditions, in some position the few character should be excluded.

f) Application should have input, to generate multiple number at single point of time.

Based on the above conditions I am providing step by step guide, the way to achieve the target.

Implementation

Step 1: We have to create a java project in eclipse or NetWeaver Developer Studio.

Step 2: At the time of creation the project provide some name of the project.

Step 3: Now we have to create the three java class under a package called tcs.com

Step 4:

Step 5: Now we have to implement the java class.

Step 6: Implement DTOForId.java as below.

DTOForId.java

package com.tcs;

/**

* @author user

*

*/

public class DTOForId {

    private String string_LOWER_LIMIT;

    private String string_NEXT_VALUE;

    private String string_UPPER_LIMIT;

    private String string_Loop;

    private String string_CHECK_ALPHA_NUMERIC_POSITION;

    private String string_POSITIONS_EXCLUDE;

    /**

     * @return the string_LOWER_LIMIT

     */

    public String getString_LOWER_LIMIT() {

        return string_LOWER_LIMIT;

    }

    /**

     * @param stringLOWERLIMIT the string_LOWER_LIMIT to set

     */

    public void setString_LOWER_LIMIT(String stringLOWERLIMIT) {

        string_LOWER_LIMIT = stringLOWERLIMIT;

    }

    /**

     * @return the string_NEXT_VALUE

     */

    public String getString_NEXT_VALUE() {

        return string_NEXT_VALUE;

    }

    /**

     * @param stringNEXTVALUE the string_NEXT_VALUE to set

     */

    public void setString_NEXT_VALUE(String stringNEXTVALUE) {

        string_NEXT_VALUE = stringNEXTVALUE;

    }

    /**

     * @return the string_UPPER_LIMIT

     */

    public String getString_UPPER_LIMIT() {

        return string_UPPER_LIMIT;

    }

    /**

     * @param stringUPPERLIMIT the string_UPPER_LIMIT to set

     */

    public void setString_UPPER_LIMIT(String stringUPPERLIMIT) {

        string_UPPER_LIMIT = stringUPPERLIMIT;

    }

    /**

     * @return the string_Loop

     */

    public String getString_Loop() {

        return string_Loop;

    }

    /**

     * @param stringLoop the string_Loop to set

     */

    public void setString_Loop(String stringLoop) {

        string_Loop = stringLoop;

    }

    /**

     * @return the string_CHECK_ALPHA_NUMERIC_POSITION

     */

    public String getString_CHECK_ALPHA_NUMERIC_POSITION() {

        return string_CHECK_ALPHA_NUMERIC_POSITION;

    }

    /**

     * @param stringCHECKALPHANUMERICPOSITION

the string_CHECK_ALPHA_NUMERIC_POSITION to set

     */

    public void setString_CHECK_ALPHA_NUMERIC_POSITION(

            String stringCHECKALPHANUMERICPOSITION) {

        string_CHECK_ALPHA_NUMERIC_POSITION = stringCHECKALPHANUMERICPOSITION;

    }

    /**

     * @return the string_POSITIONS_EXCLUDE

     */

    public String getString_POSITIONS_EXCLUDE() {

        return string_POSITIONS_EXCLUDE;

    }

    /**

     * @param stringPOSITIONSEXCLUDE the string_POSITIONS_EXCLUDE to set

     */

    public void setString_POSITIONS_EXCLUDE(String stringPOSITIONSEXCLUDE) {

        string_POSITIONS_EXCLUDE = stringPOSITIONSEXCLUDE;

    }

 

 

}

Step 7: Implementation of AlphanumericSorting.java as below.

AlphanumericSorting.java

package com.tcs;

import java.util.Comparator;

/**

* @author user

*

*/

public class AlphanumericSorting implements Comparator<Object>{

     /* (non-Javadoc)

     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

     */

    public int compare(Object firstObjToCompare, Object secondObjToCompare) {

            String firstString = firstObjToCompare.toString();

            String secondString = secondObjToCompare.toString();

  

            if (secondString == null || firstString == null) {

                return 0;

            }

  

            int lengthFirstStr = firstString.length();

            int lengthSecondStr = secondString.length();

  

            int index1 = 0;

            int index2 = 0;

  

            while (index1 < lengthFirstStr && index2 < lengthSecondStr) {

                char ch1 = firstString.charAt(index1);

                char ch2 = secondString.charAt(index2);

  

                char[] space1 = new char[lengthFirstStr];

                char[] space2 = new char[lengthSecondStr];

  

                int loc1 = 0;

                int loc2 = 0;

  

                do {

                    space1[loc1++] = ch1;

                    index1++;

  

                    if (index1 < lengthFirstStr) {

                        ch1 = firstString.charAt(index1);

                    } else {

                        break;

                    }

                } while (Character.isDigit(ch1) == Character.isDigit(space1[0]));

  

                do {

                    space2[loc2++] = ch2;

                    index2++;

  

                    if (index2 < lengthSecondStr) {

                        ch2 = secondString.charAt(index2);

                    } else {

                        break;

                    }

                } while (Character.isDigit(ch2) == Character.isDigit(space2[0]));

  

                String str1 = new String(space1);

                String str2 = new String(space2);

  

                int result;

  

                if (Character.isDigit(space1[0]) && Character.isDigit(space2[0])) {

                    Integer firstNumberToCompare = new Integer(Integer

                            .parseInt(str1.trim()));

                    Integer secondNumberToCompare = new Integer(Integer

                            .parseInt(str2.trim()));

                    result = firstNumberToCompare.compareTo(secondNumberToCompare);

                } else {

                    result = str1.compareTo(str2);

                }

  

                if (result != 0) {

                    return result;

                }

            }

            return lengthFirstStr - lengthSecondStr;

        }

 

}

Step 8: Implementation of TestClient.java as below.

TestClient.java

package com.tcs;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

public class TestClient {

 

    public List<String> generate_UNIQUE_ID(DTOForId dtoForId) {

        // TODO Auto-generated method stub

        String string_Prog_1 = dtoForId.getString_CHECK_ALPHA_NUMERIC_POSITION(); // This value should come from DB config

        String string_Prog_2 = dtoForId.getString_POSITIONS_EXCLUDE(); // This Value should come from DB config

        String string_LOWER_LIMIT = dtoForId.getString_LOWER_LIMIT(); // This Value should come from DB config

        String string_UPPER_LIMIT = dtoForId.getString_UPPER_LIMIT(); // This Value should come from DB config

        String string_NEXT_VALUE = dtoForId.getString_NEXT_VALUE(); // This Value should come from DB config

        String string_Loop = dtoForId.getString_Loop(); // This value should pass by application,

                                                                                     //how many numbers should get generate @ each transaction

     

        HashMap<String, String> hashMap_CHECK_ALPHA_NUMERIC = new HashMap<String, String>();

        HashMap<String, ArrayList<String>> hashMap_Excelude = new HashMap<String, ArrayList<String>>();

     

        if(string_Prog_1 != null && !"".equals(string_Prog_1)){

            String[] strings = string_Prog_1.split(" ");

            for (String string : strings) {

                String string_Key = string.substring(0,string.indexOf("-"));

                if(string_Key != null && !"".equals(string_Key)){

                    int i = 0;

                    i = Integer.parseInt(string_Key);

                    if(i > 0){

                        i  = i - 1;

                        string_Key = String.valueOf(i);

                    }

                }

                String string_Value = string.substring(string.indexOf("-")+1);

                hashMap_CHECK_ALPHA_NUMERIC.put(string_Key, string_Value);

            }

        }

     

        if(string_Prog_2 != null && !"".equals(string_Prog_2)){

            String[] strings = string_Prog_2.split(" ");

            for (String string2 : strings) {

                String string_Key = string2.substring(0,string2.indexOf("-"));

                if(string_Key != null && !"".equals(string_Key)){

                    int i = 0;

                    i = Integer.parseInt(string_Key);

                    if(i > 0){

                        i  = i - 1;

                        string_Key = String.valueOf(i);

                    }

                }

                String string_Value = string2.substring(string2.indexOf("-")+1);

                ArrayList<String> arrayList = hashMap_Excelude.get(string_Key);

                if(arrayList == null){

                    arrayList = new ArrayList<String>();

                }

                arrayList.add(string_Value);

                hashMap_Excelude.put(string_Key, arrayList);

            }

        }

     

        // CHECK THE POSTION EXCLUDE WITH CONFIG DATA

        boolean b_Lower_Check = false;

        int i_lower_length = string_LOWER_LIMIT.length();

        for (int i = 0; i < i_lower_length; i++) {

            ArrayList<String> arrayList= hashMap_Excelude.get(String.valueOf(i));

            char char_Value = string_LOWER_LIMIT.charAt(i);

            if(arrayList != null && arrayList.size() > 0){

            for (String string3 : arrayList) {

                if(string3.equalsIgnoreCase(String.valueOf(char_Value))){

                    b_Lower_Check = true;

                    break;

                }

            }

            }

         

        }

        if(b_Lower_Check){

         

            return null;

        }

        boolean b_Upper_Check = false;

        int i_upper_length = string_UPPER_LIMIT.length();

        for (int i = 0; i < i_upper_length; i++) {

            ArrayList<String> arrayList= hashMap_Excelude.get(String.valueOf(i));

            char char_Value = string_UPPER_LIMIT.charAt(i);

            if(arrayList != null && arrayList.size() > 0){

            for (String string : arrayList) {

                if(string.equalsIgnoreCase(String.valueOf(char_Value))){

                    b_Upper_Check = true;

                    break;

                }

            }

            }

        }

        if(b_Upper_Check){

         

            return null;

        }

        // FORMAT CHECKING

        int i_LENGTH_FORMAT = string_LOWER_LIMIT.length();

     

        char[] cs_char_FORMAT = new char[i_LENGTH_FORMAT];

     

     

        for (int j = 0; j < i_LENGTH_FORMAT; j++) {

            cs_char_FORMAT[j] = string_NEXT_VALUE.charAt(j);

         

        }

     

     

        boolean b_FORMAT = false;

        for (int i_Format = 0; i_Format < cs_char_FORMAT.length; i_Format++) {

            char c  = cs_char_FORMAT[i_Format];

         

            if(hashMap_CHECK_ALPHA_NUMERIC != null

&& hashMap_CHECK_ALPHA_NUMERIC.size() > 0){

         

            String string_Value = hashMap_CHECK_ALPHA_NUMERIC.get(String.valueOf(i_Format));

         

            if(string_Value != null && !"".equals(string_Value)

                    && string_Value.equalsIgnoreCase("A") && Character.isDigit(c)){

                b_FORMAT = true;

            }else if(string_Value != null && !"".equals(string_Value)

                    && string_Value.equalsIgnoreCase("N") && Character.isLetter(c)){

                b_FORMAT = true;

            }

            }

        }

    

        if(b_FORMAT){

     

        return null;

        }

                                           

        List<String> linkedList = new ArrayList<String>();

     

        if(!b_FORMAT){

        if(string_NEXT_VALUE.equalsIgnoreCase(string_UPPER_LIMIT)){

            linkedList.add(string_LOWER_LIMIT);

        }else{

            linkedList = getNumbers(string_LOWER_LIMIT, string_NEXT_VALUE, string_UPPER_LIMIT,

                    string_Loop, hashMap_Excelude,hashMap_CHECK_ALPHA_NUMERIC);

        }

     

        String[] strings = new String[linkedList.size()];

        if(linkedList != null && linkedList.size() > 0){

            for (int i_Last = 0; i_Last < linkedList.size(); i_Last++) {

                strings[i_Last] = linkedList.get(i_Last);

            }

        }

        if(strings != null && strings.length > 0){

            Arrays.sort(strings, new AlphanumericSorting());

        }

     

        linkedList = Arrays.asList(strings);

     

    }

        if(linkedList == null || linkedList.size() == 0){

            return null;

        }

     

     return linkedList;

     

    }

 

    /**

     * @param string

     * @return

     */

    private char[] char_Array(String string){

     

        int i_LENGTH = string.length();

     

        char[] cs_char = new char[i_LENGTH];

     

        int i_COUNTER = 0;

        for (int j = i_LENGTH -1; j > -1; j--) {

            cs_char[i_COUNTER] = string.charAt(j);

            i_COUNTER++;

        }

        return cs_char;

    }

    /**

     * @param string_Lower

     * @param string_Next

     * @param string_Upper

     * @return

     */

    private String numbers(String string_Lower,String string_Next,

String string_Upper,HashMap<String, ArrayList<String>> hashMap_Excelude,

             HashMap<String, String> hashMap_CHECK_ALPHA_NUMERIC){

     

        try {

            char[] char_lower = char_Array(string_Lower);

            char[] char_next = char_Array(string_Next);

            char[] char_upper = char_Array(string_Upper);

         

            StringBuilder stringBuilder = new StringBuilder(); 

            int i_LENGTH = string_Next.length();

         

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

                char c_lower = char_lower[i];

                char c_next = char_next[i];

                char c_upper = char_upper[i];

             

             

                if(Character.isLetter(c_lower) && Character.isLetter(c_next)

&& Character.isDigit(c_upper)){

                    c_next++;

                    if(c_next > 90){

                        char c = '0';

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                     

                        stringBuilder.append(String.valueOf(c));

                        break;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }else if(Character.isLetter(c_lower) && Character.isDigit(c_next)

&& Character.isDigit(c_upper)){

                    c_next++;

                    if(c_next > c_upper){

                     

                        char c = c_lower;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                     

                     

                        stringBuilder.append(c);

                        continue;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }else if(Character.isDigit(c_lower) && Character.isLetter(c_next)

&& Character.isLetter(c_upper)){

                    c_next++;

                    if(c_next > c_upper){

                        char c = c_lower;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i), String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        continue;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }else if(Character.isDigit(c_lower) && Character.isDigit(c_next)

&& Character.isLetter(c_upper)){

                    c_next++;

                    if(c_next > 57){

                        char c = 'A';

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }else if(Character.isDigit(c_lower) && Character.isDigit(c_next)

&& Character.isDigit(c_upper)){

                    c_next++;

                    if(c_next > c_upper){

                        char c = c_lower;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        continue;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }else if(Character.isLetter(c_lower) && Character.isLetter(c_next)

&& Character.isLetter(c_upper)){

                    c_next++;

                    if(c_next > c_upper){

                        char c = c_lower;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        continue;

                    }else{

                        char c = c_next;

                        boolean b_check = true;

                        while(b_check){

                            b_check = check(hashMap_Excelude, String.valueOf(i),

String.valueOf(i_LENGTH), String.valueOf(c));

                            if(b_check){

                                c++;

                            }

                        }

                        stringBuilder.append(c);

                        break;

                    }

                }

            }

            if(stringBuilder != null){

                String string = stringBuilder.reverse().toString();

                String string_1 = string_Next.substring(0, string_Next.length() - string.length());

                string_Next = string_1+string;

             

            }

            boolean b_FORMAT = check_Format(string_Next,hashMap_CHECK_ALPHA_NUMERIC);

             

                if(b_FORMAT){

                        string_Next = numbers(string_Lower, string_Next, string_Upper,

hashMap_Excelude, hashMap_CHECK_ALPHA_NUMERIC);

                }

         

         

        } catch (Throwable e) {

            // TODO Auto-generated catch block

            if (e instanceof Error) {

                numbers(string_Lower, string_Next, string_Upper,

hashMap_Excelude, hashMap_CHECK_ALPHA_NUMERIC);     

            }

        }

        return string_Next;

    }

    /**

     * @param string_TYPE

     * @param hashMap_CHECK_ALPHA_NUMERIC

     * @return

     */

    private boolean check_Format(String string_Next,

HashMap<String, String> hashMap_CHECK_ALPHA_NUMERIC){

        boolean b_FORMAT = false;

        int i_LENGTH_FORMAT = string_Next.length();

     

        char[] cs_char_FORMAT = new char[i_LENGTH_FORMAT];

     

     

        for (int j = 0; j < i_LENGTH_FORMAT; j++) {

            cs_char_FORMAT[j] = string_Next.charAt(j);

         

        }

     

     

        for (int i_Format = 0; i_Format < cs_char_FORMAT.length; i_Format++) {

            char c  = cs_char_FORMAT[i_Format];

         

            if(!hashMap_CHECK_ALPHA_NUMERIC.isEmpty()){

         

            String string_Value = hashMap_CHECK_ALPHA_NUMERIC.get(String.valueOf(i_Format));

         

            if(string_Value != null && !"".equals(string_Value)

                    && string_Value.equalsIgnoreCase("A") && Character.isDigit(c)){

                string_Next = replaceCharAt(string_Next, i_Format, 'A');

                b_FORMAT = true;

                break;

            }else if(string_Value != null && !"".equals(string_Value)

                    && string_Value.equalsIgnoreCase("N") && Character.isLetter(c)){

                string_Next = replaceCharAt(string_Next, i_Format, '0');

                b_FORMAT = true;

                break;

            }

            }

        }

     

 

        return b_FORMAT;

    }

    /**

     * @param string_Lower

     * @param string_Next

     * @param string_Upper

     * @param string_Loop

     * @return

     */

    private List<String> getNumbers(String string_Lower,String string_Next,

String string_Upper, String string_Loop, HashMap<String, ArrayList<String>> hashMap_Excelude,

HashMap<String, String> hashMap){

        List<String> list = new ArrayList<String>();

     

        int i = Integer.parseInt(string_Loop);

        for (int j = 0; j < i; j++) {

            string_Next = numbers(string_Lower, string_Next,

string_Upper,hashMap_Excelude,hashMap);

            list.add(string_Next);

        }

     

        return list;

    }

    /**

     * @param hashMap_Excelude

     * @param string_Postion

     * @return

     */

    private boolean check(HashMap<String, ArrayList<String>> hashMap_Excelude,

String string_Postion,String string_Length,String string_Value){

        boolean b = false;

        int i_LENGTH = Integer.parseInt(string_Length);

        int i = Integer.parseInt(string_Postion);

        ArrayList<String> arrayList = hashMap_Excelude.

get(String.valueOf(i_LENGTH - (i+1)));

        if(arrayList != null && arrayList.size() > 0){

            for (String string : arrayList) {

                if(string.equalsIgnoreCase(string_Value)){

                    b = true;

                    break;

                }

            }

        }

     

        return b;

    }

    /**

     * @param s

     * @param pos

     * @param c

     * @return

     */

    private String replaceCharAt(String s, int pos, char c) {

        return s.substring(0, pos) + c + s.substring(pos + 1);

      }

 

 

    public static void main(String args[]){

        TestClient helperBean = new TestClient();

     

     

        DTOForId dtoForId = new DTOForId();

        dtoForId.setString_LOWER_LIMIT("A0A000");

        dtoForId.setString_NEXT_VALUE("A0A0ZY");

        dtoForId.setString_UPPER_LIMIT("S9ZZZZ");

        dtoForId.setString_Loop("20");

        dtoForId.setString_CHECK_ALPHA_NUMERIC_POSITION("1-A 2-N"); // Delimiter of the string object is space " ". And position 1 should always be

                                                                                                                          // Alpha like A,BC, etc and position 2 should always be numeric like 0, 1, 2 etc

        dtoForId.setString_POSITIONS_EXCLUDE("5-3 6-6 6-C"); //Delimiter of the string object is space " ". And potion 5 should exclude 3, position 6

                                                                                                       // should exclude 6 and C.     

        List<String> list = helperBean.generate_UNIQUE_ID(dtoForId);

        for (String string2 : list) {

            System.out.println(string2);

        }

    }

}

Step 9: Output

Labels in this area