CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    London, ON

    [RESOLVED] Help! Password Generator

    Hi, so I am a student in Computer Programming, and I have a project due this morning.
    I'm working on a password generator that fulfils these requirements:

    Code must compile and run without crashing.

    Program must explain what it will do and tell user what the acceptable
    range of inputs is. Data validation of user input must be done.

    Loop structure must be set up to generate characters. Characters
    generated must be in the proper range and validated before being placed
    in the array.

    Candidate password must be tested on all five conditions. Successful
    candidate must be converted to a String object for output.

    Final output must show the valid generated password and tell how many
    iterations it took to generate the password.

    The problems I am having are that my program is:
    - Restating the statement "Your password is".
    - Outputting random characters that I did not put in the hex characters.

    Here is my code for anyone who wants to look it over:

    Start of Code

    * Program Name:T_H_PasswordGenerator.java
    * Purpose: Generate a random password
    * Coder:

    import java.util.Scanner;
    public class T_H_PasswordGenerator

    public static void main(String[] args)

    Scanner input = new Scanner(System.in);
    int userChoice;
    int randomHex;

    // Tell the user what the program does.
    System.out.println("This program will randomly generate a password for you");

    // Prompt the user.
    System.out.println("Please enter how many characters do you want in your password? (6 - 12 characters)");
    userChoice = input.nextInt();

    // Create hex characters.
    char[] arrayHex = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
    '$','#','@','*','/','^' };

    // Start of loop.

    char [] passwordArray = new char[userChoice];

    for(char i = 0; i < passwordArray.length; i++)
    passwordArray[i] = (char)(Math.random() * 122 + 33);

    System.out.print("your password is " + passwordArray[i] );

    // End of loop.


    End of Code

    If anyone can help me, it'd be greatly appreciated!

  2. #2
    Join Date
    Aug 2011
    West Yorkshire, U.K.

    Re: Help! Password Generator

    Problem 1: The line which prints the password is inside the loop - so as the password array is populated with each character, it will be printed out.

    Problem 2: You are populating the password array with random chars from the range 33 (!) to (33 + 122 = 155) - but characters above 126 are 8 bit characters and may well display as hieroglyphics.

    It looks like you initialise an array with the legal characters so you should be selecting a random character from that array. Try generating a random number N between 0 and length of arrayHex, then add arrayHex[N] to the password array.

  3. #3
    Join Date
    Nov 2011
    London, ON

    Re: Help! Password Generator

    Thank you for replying!

    I understand what you're saying, but I'm not quite sure how to go about it. I'm a novice at this type of stuff. I know I have to put the statement outside of the loop, but then I have to make a new string, right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.