I need to be able to create a function that generates a selected number of strings with randomly generated positive numbers added into them that are based on a string mask.
Example of a string mask where [n#] represents a positive number with a certain number of digits:
generateStrings(2, "( (-[n2]) + [n5] ) / [n1]");
The first number tells the function how many strings to generate.
2 generated strings:
( (-23) + 47269 ) / 9
( (-12) + 17935 ) / 1
I'd like to be able to generate strings with numbers ranging from 1 digit to 10 digits.
EDIT:Here is a function that can generate a number with digits ranging from 1 to 10:
public static int generateNumber(int n) {
int m;
if (n==1){
m = (0 + (int)(Math.random() * ((9 - 0) + 1)));
} else if (n==2) {
m = (10 + (int)(Math.random() * ((99 - 10) + 1)));
} else if (n==3) {
m = (100 + (int)(Math.random() * ((999 - 100) + 1)));
} else if (n==4) {
m = (1000 + (int)(Math.random() * ((9999 - 1000) + 1)));
} else if (n==5) {
m = (10000 + (int)(Math.random() * ((99999 - 10000) + 1)));
} else if (n==6) {
m = (100000 + (int)(Math.random() * ((999999 - 100000) + 1)));
} else if (n==7) {
m = (1000000 + (int)(Math.random() * ((9999999 - 1000000) + 1)));
} else if (n==8) {
m = (10000000 + (int)(Math.random() * ((99999999 - 10000000) + 1)));
} else if (n==9) {
m = (100000000 + (int)(Math.random() * ((999999999 - 100000000) + 1)));
} else if (n==10) {
m = (1000000000 + (int)(Math.random() * ((2147483647 - 1000000000) + 1)));
}
return m;
}
Now I just need to be able to apply that function to a string mask.
EDIT3: Here is a script that should generate a single string with a string mask in the format above:
public static String generateString(String mask) {
for (int i = 1; i < 10; i++)
{
String searchString = "[n" + i + "]";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = mask.indexOf(searchString,lastIndex);
if( lastIndex != -1){
count ++;
lastIndex+=searchString.length();
}
}
for (int j=count; j > 0;) {
while(lastIndex != -1){
lastIndex = mask.indexOf(searchString,lastIndex);
if( lastIndex != -1){
count ++;
lastIndex+=searchString.length();
}
}
mask = mask.replaceFirst(searchString, String.valueOf(generateNumber(i)));
}
}
return mask;
}
I don't know if this script would work, and I don't know how to test my code, so I would appreciate it if someone would verify if it works. Part of this code was from ansible's answer, and another part is from codebreach's answer to this question: Occurrences of substring in a string I just want to give them credit for the work that they did.