简体   繁体   中英

I'm trying to make a java program that creates passwords

I think I have everything working except for one thing. When I call the method more than once on main it keeps creating the same password.

Here's the class for the password creation:

import java.util.Random;

public class PasswordRandomizer {
    // Define the variables
    private int length;
    private String password;
    private Random random = new Random();
    private char symbol;

    public PasswordRandomizer(int length) {
        // Initialize the variable
        password = "";
        this.length = length;
        while (this.password.length() < this.length) {
            this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(25));
            this.password += symbol;
        }
    }

    public String createPassword() {
        // write code that returns a randomized password
        return this.password;
    }
}

and this is what I have in main:

public 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());
    }
}

I would get an output like this:

Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo

Feel free to point out any other mistakes or bad habits I have, I'm still pretty new at this.

Let's look at your code.

In your constructor, you initialize your length, and then you generate your password:

public PasswordRandomizer(int length) {
    // Initialize the variable
    password = "";
    this.length = length;
    while (this.password.length() < this.length) {
        this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(25));
        this.password += symbol;
    }
}

Then, in your createPassword method, you just return that password you generated in the constructor, without changing it:

public String createPassword() {
    // write code that returns a randomized password
    return this.password;
}

And so, every time you call createPassword , you are going to get the same thing. Let's see what happens if we just move that code which generates the password down into the createPassword method:

import java.util.Random;

public class PasswordRandomizer {
    // Define the variables
    private int length;
    private String password;
    private Random random = new Random();
    private char symbol;

    public PasswordRandomizer(int length) {
        // Initialize the variable
        this.length = length;
    }

    public String createPassword() {
        // write code that returns a randomized password
        password = "";
        while (this.password.length() < this.length) {
            this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(26));
            this.password += symbol;
        }
        return this.password;
    }

}

Now, when we run your Program , you get output like:

Password: mvlqqgfmotldc
Password: inneuyuynqakd
Password: hstlfsfspfaua
Password: jgngsmdiguxcy

You should create a new PasswordRandomizer program, otherwise, you will not create a password each time. For instance, if you don't rewrite the PasswordRandomizer class, you can do :

package test;
public class Program {
    public static void main(String[] args) {
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
    }
}

You could use the Apache Commons Lang library.

In the RandomStringUtils there is a method to generate a random String with a certain length: randomAlphabetic(int count)

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html#randomAlphabetic-int-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM