I have a method to generate a random password recursively, which is required to have at least one uppercase letter. I have the method set up, but for some reason it's telling me that the method must have a return of type String when I've already defined a return
type in my if statement.
Question: How can I make this error go away or fix my method in order to have it return a password?
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 String generatePassword(Random rand, String password, int position, int size) //this method must return a result of type String
{
boolean isLowerCase = rand.nextBoolean();
int randomChar = rand.nextInt(chars.length);
char c = chars[randomChar];
if(position == size) //base case
{
return password; //string is returned here?
}
if(isLowerCase)
{
generatePassword(rand, password + c, position + 1, size);
}
else //its either upper or lower case
{
generatePassword(rand, password + Character.toUpperCase(c), position + 1, size);
}
Aucun commentaire:
Enregistrer un commentaire