Issue
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();
}
Solution
Here you are, this algorithm is universal. It will work for every value, not only for 9 characters.
public class Algorithm {
private static final String CHARACTERS = "ABCDEFGHIJKLMONPQRSTUVWXYZ0123456789";
public static void main(String[] args) {
System.out.println(getRandomCode(9));
System.out.println(getRandomCode(12));
System.out.println(getRandomCode(15));
System.out.println(getRandomCode(50));
}
public static String getRandomCode(int numberOfCharacters) {
StringBuilder result = new StringBuilder();
Random random = new Random();
for (int index = 1; index <= numberOfCharacters; index++) {
result.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
if (index % 3 == 0 && index != numberOfCharacters) {
result.append("-");
}
}
return result.toString();
}
}
It will print:
8PJ-Y6T-4LV
FE7-WRY-64A-2L2
23H-A24-CBF-E8Z-NHD
DXA-8Z3-DB4-2ZS-U2O-SQS-CAR-Y5Z-UXT-AP9-3TI-9ZO-D7T-OMZ-RDN-K34-BB
Also if you want to try the same with while loop you can first split string by 3, add results to the list and then join them with String.join:
public static String getRandomCode(int numberOfCharacters) {
List<String> results = new ArrayList<>();
StringBuilder result = new StringBuilder();
Random random = new Random();
int index = 0;
int prevIndex = 0;
while (index < numberOfCharacters) {
index++;
result.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
if (index % 3 == 0) {
results.add(result.substring(prevIndex, index));
prevIndex = index;
}
}
return String.join("-", results);
}
And if you want to be super elastic you can also parametrize split point, so not only by 3, but also any other value:
private static final String CHARACTERS = "ABCDEFGHIJKLMONPQRSTUVWXYZ0123456789";
public static void main(String[] args) {
System.out.println(getRandomCode(9,3));
System.out.println(getRandomCode(12,4));
System.out.println(getRandomCode(15,5));
System.out.println(getRandomCode(150,50));
}
public static String getRandomCode(int numberOfCharacters, int splitPointIndex) {
List<String> results = new ArrayList<>();
StringBuilder result = new StringBuilder();
Random random = new Random();
int index = 0;
int prevIndex = 0;
while (index < numberOfCharacters) {
index++;
result.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
if (index % splitPointIndex == 0) {
results.add(result.substring(prevIndex, index));
prevIndex = index;
}
}
return String.join("-", results);
}
It will print:
HBZ-BWT-MYI
7YSM-DD0K-HJYF
EUQ2P-NFJ4F-L4MRP
MG91WG9F9HF1GM0UH6I91VX7TLNIUDPQUCUK8SXDQ3T087JPAE-JJ6IZHKH2YBZBJKPPHJFR43112JKQDBJ9LSTAA7BTB6O3JXTYX-EGCJBGWJBZJOGBMVCH9TLCL9VSV3L15JR7QZIKIKOZGAOKR6GT
Cheers!
Answered By - MrFisherman