|
If you are looking for an example to generate a random password then below example will help you. In this example the word range will be 0-9, A-Z and a-z. Since we want the combination of this characters, we have put a condition where we are checking for the integer value to be in range of 48 - 57 or 65 - 90 or 97 - 122. These integer values represents the ASCII values for the above word range.
Please refer the ASCII chart.
import java.util.Random;
public class RandomPassword {
public static void main(String[] args) {
Random randomNumber = new Random();
char password[] = new char[15];
for (int i = 0; i < 15;) {
int value = randomNumber.nextInt(125);
System.out.println(value);
if ((value >= 48 && value <= 57) || (value >= 65 && value <= 90)
|| (value >= 97 && value <= 122)) {
password[i] = (char) value;
i++;
}
}
System.out.println("Your password is " + new String(password));
}
}
|