Examples
- A00099 -> A00100
- AB0323 -> AB0324
// Method to get next AlphaNumeric String
public String getNextAlphaNumeric(String oldAlphaNumericString)
{
// number of alphabets in alphanumeric string
int noOfAlphabets = 1;
String subStringAlphabet = oldAlphaNumericString.substring(0, noOfAlphabets);
String subStringNoAlpha = oldAlphaNumericString.substring(noOfAlphabets);
String leadingZeros = "";
String noLeadingZeros = "";
String newAlphaNumeric = "";
// To count number of leading zeros
for (int i = 0; i < subStringNoAlpha.length(); i++)
{
if (subStringNoAlpha.charAt(i) == '0')
{
leadingZeros += subStringNoAlpha.charAt(i);
}
else
{
break;
}
}
if (leadingZeros.length() > 0)
{
// number to be added
int addToAN = 1;
noLeadingZeros += subStringNoAlpha.substring(leadingZeros.length());
String noLeadingZeroNewAN = "" + (Long.parseLong(noLeadingZeros) + addToAN);
if (noLeadingZeroNewAN.length() != noLeadingZeros.length())
{
// To check whether number of digits changed. Eg : 99 to 100
int diff = noLeadingZeroNewAN.length() - noLeadingZeros.length();
for (int i = 0; i < leadingZeros.length() - diff; i++)
{
newAlphaNumeric += leadingZeros.charAt(i);
}
newAlphaNumeric += "" + noLeadingZeroNewAN;
}
else
{
for (int i = 0; i < leadingZeros.length() - noOfAlphabets; i++)
{
newAlphaNumeric += leadingZeros.charAt(i);
}
newAlphaNumeric += "" + noLeadingZeroNewAN;
}
}
else
{
newAlphaNumeric += "" + (Long.parseLong(subStringNoAlpha) + 1);
}
return subStringAlphabet + newAlphaNumeric;
}