I am generating a random password. My password is 8 characters in length and it includes the special characters. I need to keep the first letter as alphabet and need to shuffle the remaining seven characters so that it will be a mixture of alphanumeric + ascii characters.
public String generatePassword() {
int passwordLength = MAX_PASSWORD_LENGTH;
StringBuffer password = new StringBuffer(passwordLength);
//first character as an alphabet
password.append(RandomStringUtils.randomAlphabetic(1)).toString();
String alphaNumeric = RandomStringUtils.random(5, true, true);
String asciiChars = RandomStringUtils.randomAscii(2);
password.append(alphaNumeric).append(asciiChars);
return password.toString();
}
I need some help to shuffle the last 7 characters. How to do it?
The Java Collections API has an inbuilt shuffle method that you can use: see here.
Basically, you need to create a
Listfrom the last 7 characters, and pass it toCollections.shuffle.