This should do it mate:
Java5 Code:
public class GenerateString {
private static int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
private static int randomNum, randStrLen = 8;
public static String generateStr
(){ random = "";
for (int i = 0; i < randStrLen; i++){
randomNum =
(int)(Math.
random() *
(myArray.
length -
1));
random += myArray[randomNum];
}
return random;
}
public static void main (String[] args){
System.
out.
println(generateStr
());
}
}
Or if you want a re-usable function try this:
Java5 Code:
public class GenerateString {
// The generateStr function takes 2 arguments
// and integer array for the array to generate the number from
// and an integer, wich is the length of the string to be generated
// this function can be called like so: generateStr(myArray, 8)
// this function returns a String value
public static String generateStr
(int[] arrayTarget,
int randStrLen
){ int randomNum;
for (int i = 0; i < randStrLen; i++){
randomNum =
(int)(Math.
random() *
(arrayTarget.
length -
1));
random += arrayTarget[randomNum];
}
return random;
}
public static void main (String[] args){
int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
System.
out.
println(generateStr
(myArray,
8));
}
}
A nother option would be to create an object... But i dont think it would be practical.