Java

Generating Next Alpha Numberic ID in Java

This blog post we are going to talk about how to generate next alpha numeric ID while working in project using simple String methods.

Let say last generated id was CWZ00023 and now you need to generate the next ID that is CWZ00024.

Following is the code to generate the same

//CWZ00001


class GenerateNextID
{
    public static void main(String[] args)
    {

        String prevId="CWZ00023";

        String data=prevId.replace("CWZ","");

        int num=Integer.parseInt(data);

        num=num+1;

        String nextID=getNumberingFormat(num);

        System.out.println(nextID);

    }


    public static String getNumberingFormat(int number)
    {
        String numberData="CWZ";
        if(number>=1&&number<=9)
        {
            numberData=numberData+"0000"+number;
        }

        else if(number>=10&&number<=99)
        {
            numberData=numberData+"000"+number;
        }

        else if(number>=100&&number<=999)
        {
            numberData=numberData+"00"+number;
        }

        else if(number>=1000&&number<=9999)
        {
            numberData=numberData+"0"+number;
        }

        else if(number>=10000&&number<=99999)
        {
            numberData=numberData+""+number;
        }

        return numberData;


    }
    
}

You can watch the execution of the above code in the following video.

Related Posts

Table Of Contents

;