I've decided to ask advice more skilled coders. I need to write a method that generates a random collection of strings, each of which is a string that contains from 6 to 15 characters long. The number of strings in the collection is passed as a parameter to the method. I think that strings should have different characters.
I tried, but there is a mistake in something. The collection gives the same strings and the same characters set. And the quantity of characters in a string conforms to quantity of characters. I change "int len" on 5 for example then I receive: 5 strings in a collection and 5 characters in a string.
public static void main(String[] args) {
System.out.println(generateRandomString(5, 5));
}
public static String generateRandomString(int len, int count) {
String letters = new String("abcdefghijklmnopqrstuvwxyz");
StringBuffer word = new StringBuffer();
List<StringBuffer> randomWords = new LinkedList<StringBuffer>();
int randomIndex = 0;
for (int i = 0; i < len; i++) {
randomIndex = new Random().nextInt(letters.length());
word.append(letters.charAt(randomIndex));
for (int j = 0; j < randomWords.size() && j <= count; j++);
randomWords.add(word);
}
return randomWords.toString();
}
Aucun commentaire:
Enregistrer un commentaire