I am attempting to write a recursive password generating program in java, but instead of returning one randomly generated password, the console spits out every combination of passwords and just keeps going.
*Also, I'm required to have at least one capital letter in the password, which is why I am randomly generating either one or zero.
Question: How can I make this recursive method only print out ONE randomly generated password of randomSize
length? How can I make the char upper case depending on whether or not randomCapital
is 1?
CODE:
public class PasswordGenerator{
public static void main(String[] args) {
Random rand = new Random();
int randomSize = rand.nextInt((13) + 8);
generatePassword("", 0, randomSize);
}
public static char[]chars = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5',
'6','7','8','9','!','@','$','%','^','&'};
public static void generatePassword(String password, int position, int size)
{
Random rand = new Random();
int randomCapital = rand.nextInt(1);
if(position < size && randomCapital == 0)
{
for(char ch: chars)
{
generatePassword(password + ch, position + 1, size);
}
if(position < size && randomCapital == 1)
{
for(char ch: chars)
{
//how can I make this char capital if randomCapital is 1?
generatePassword(password + ch, position + 1, size);
}
}
}
else
System.out.println(password);
}
}
Aucun commentaire:
Enregistrer un commentaire