A large part of your id (8 of 13 characters) is actually static.
That leaves you with 260.000 variations (10 (0-9) x 26 (A-Z) x 1000 (0-999)) of ids to assign to your users. After that, you run out of ids. Will that be enough?
In your current code there is not much you can do besides checking wether the id you generated is already in the generatedIDs list and if it is, try again.
Is there a purpose behind generating the ids randomly? Because if they are only used to identify users, it would make more sense to increment them one number at a time and just remember the last id you gave to a user.
User 1: (0)(0)(A)(000)98XX001
User 2: (0)(0)(A)(001)98XX001
User 5000: (0)(0)(E)(999)98XX001
User 80543: (0)(3)(B)(542)98XX001
User 260000: (0)(9)(Z)(999)98XX001
Here would be the code for that. You hand the function the id you would like to start from and how many ids from that start-id it should generate.
import java.util.ArrayList;
import java.util.List;
public class MainClass
{
public static void main(String[] args)
{
TestClass gen = new TestClass();
gen.generateIDs("04X99798XX001", 10);
}
}
public class TestClass
{
public static Integer letterToInt(char letter) {
return letter - 'A' + 1;
}
public static char intToLetter(int number) {
return (char) (90 - (26 - number));
}
private static List<String> generatedIDs = new ArrayList<String>();
public static void generateIDs(String lastID, Integer numOfUsers) {
String first = lastID.substring(1, 2);
char second = lastID.substring(2, 3).charAt(0);
String third = lastID.substring(3, 6);
Integer firstNum = Integer.parseInt(first);
Integer secondNum = letterToInt(second);
Integer thirdNum = Integer.parseInt(third);
for (Integer i = new Integer(0); i < numOfUsers; i++) {
thirdNum = new Integer(thirdNum.intValue() + 1);
if (thirdNum > 999) {
thirdNum = new Integer(0);
secondNum = new Integer(secondNum.intValue() + 1);
}
if (secondNum > 26) {
secondNum = new Integer(1);
firstNum = new Integer(firstNum.intValue() + 1);
}
if (firstNum > 9) {
// Ran out of IDs, do something.
return;
}
first = String.valueOf(firstNum);
second = intToLetter(secondNum);
third = String.valueOf(thirdNum);
while (third.length() < 3) {
third = "0" + third;
}
String nextID = "0" + first + intToLetter(secondNum) + String.valueOf(third) + "98XX001";
generatedIDs.add(nextID);
System.out.println(nextID);
}
}
}
The result looks like this.
04X99898XX001
04X99998XX001
04Y00098XX001
04Y00198XX001
04Y00298XX001
04Y00398XX001
04Y00498XX001
04Y00598XX001
04Y00698XX001
04Y00798XX001