Brian Shook
Brian Shook

Reputation: 13

Program that creates a random string of characters and acts as a password generator

I'm writing this program that asks the user for password length and then creates a random string of characters that length using characters ! to ~. The only part that I am stuck on is that it is creating multiple strings where I only need one. Any input would be appreciated.

import java.util.*;
public class pw
{
    public static void main(String[] arg) {
        Scanner in = new Scanner(System.in);
        Random gen = new Random();
        System.out.println("Enter password length:  ");
        int length = in.nextInt();

        for (int i = 1; i < length; i++) {
            int slength = length + gen.nextInt(length - 1);
            StringBuilder s = new StringBuilder(slength);

            while (s.length() <= length) {
                s.append((char) (33 + gen.nextInt(126)));
            }
            System.out.println(s.toString());
        }
    }
}

Upvotes: 1

Views: 424

Answers (3)

Zymus
Zymus

Reputation: 1698

import java.security.SecureRandom;
import java.util.Scanner;

public class PasswordGenerator {

    public static void main(final String[] args) {
        final Scanner scanner = new Scanner(System.in);
        System.out.print("Desired password length: ");
        final int length = scanner.nextInt();

        final PasswordGenerator generator = new PasswordGenerator();
        final String password = generator.generatePassword(length);
        System.out.println("Password: " + password);
    }

    public String generatePassword(final int length) {
        final StringBuilder builder = new StringBuilder();
        final SecureRandom rng = new SecureRandom();
        for (int i = 0; i < length; i++) {
            final char value = (char) (rng.nextInt(94) + 33);
            builder.append(value);
        }
        return builder.toString();
    }
}

I changed the 126 to 94 because if you roll 125 for the random, +33 would be 158. 158 % 127 is 31, which is lower than the range of the characters you want.

You could also refactor it slightly to allow for selecting specific types of password requirements.

public String generatePassword(final int length, final char[] characters) {
    final StringBuilder builder = new StringBuilder();
    final SecureRandom rng = new SecureRandom();
    for (int i = 0; i < length; i++) {
        final char value = characters[rng.nextInt(characters.length)];
        builder.append(value);
    }
    return builder.toString();
}

Upvotes: 1

ByteHamster
ByteHamster

Reputation: 4951

This should only generate one single string:

import java.util.*;

public class pw {
    public static void main(String[]arg) {
        Scanner in = new Scanner(System.in);
        Random gen = new Random(); 
        System.out.println("Enter password length:  ");
        int length = in.nextInt();

        String s = "";
        while(s.length() <= length) {
            s += (char)(33 + gen.nextInt(126));    
        }
        System.out.println(s);
    }
}

Upvotes: 0

ThePerson
ThePerson

Reputation: 3236

You don't need the outer for loop, you are looping while the length of "s" is less than or equal to the length you require, so the outer loop isn't required.

public static void main(String[] arg) {
    Scanner in = new Scanner(System.in);
    Random gen = new Random();
    System.out.println("Enter password length:  ");
    int length = in.nextInt();

    int slength = length + gen.nextInt(length - 1);
    StringBuilder s = new StringBuilder(slength);

    while (s.length() <= length) {
        s.append((char) (33 + gen.nextInt(126)));
    }

    System.out.println(s.toString());
}

Upvotes: 1

Related Questions