lundi 4 octobre 2021

Add "-" to a random string

I want to generate a random String from 9 always separating 3 String with "-"

It must give this at the end: XXX-XXX-XXX

So far I have managed to create a Random of 9 but I don't know how to do the rest

map.put("referCode", getRandomCode(9));
public static String getRandomCode(int i) {
    final String characters = "ABCDEFGHIJKLMONPQRSTUVWXYZ0123456789";
    StringBuilder result = new StringBuilder();
    while (i > 0) {
        Random random = new Random(); 
        result.append(characters.charAt(random.nextInt(characters.length())));
        i++;
    }
    return result.toString();
}



Aucun commentaire:

Enregistrer un commentaire