The point is to make a password randomiser, but my problem is that my code makes the same password all the time. I have tried all kind of stuff and this is the latest one where I have gotten by using the Internet.
Main class:
class Program {
public static void main(String[] args) {
PasswordRandomizer randomizer = new PasswordRandomizer(13);
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
}
}
The Program
class:
import java.util.Random;
public class PasswordRandomizer {
// Define the variables
private Random password = new Random();
private int length;
private String character;
public PasswordRandomizer(int length) {
//creates a new object, uses the given password length
this.length = length;
String characters = "";
Random rndNumbers = new Random();
int randomnumber = 0;
for (int nbr = 1; nbr < length; nbr++) {
randomnumber = rndNumbers.nextInt(25);
char character = "abcdefghijklmnopqrstuvwxyz".charAt(randomnumber);
characters = characters + character;
}
System.out.println(characters);
this.character = characters;
}
public String createPassword() {
// write code that returns a randomized password
PasswordRandomizer randomizer = new PasswordRandomizer(13);
//consists of symbols a-z and is of the length given as a parameter to the constructor
return this.character;
}
}
In the PasswordRandomizer
method, there is the line System.out.println(characters);
which prints out the randomized password every time. BUT the different password doesn't come in the createPassword
method, only the first one which results in the same password all the time in the output.
Aucun commentaire:
Enregistrer un commentaire