Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Password Generating Program Help

  1. #1
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Password Generating Program Help

    I have to code a program that generates a random password for the user based on the character set they choose from. I did the code to generate the numbers (which I later have to convert to characters), but the code also asks for the password length. How do I code the part where it generates the specific length of the password?

    import java.util.Scanner;
    import java.util.Random;
    import java.io.*;
    public class Passwords
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);   
            String password = "";
            int randNum = 0;
            Random randNumList = new Random();
     
            System.out.println("Password Generation Menu");
            System.out.println();
            System.out.println("[1] Lowercase Letters");
            System.out.println("[2] Lowercase & Uppercase Letters");
            System.out.println("[3] Lowercase, Uppercase, & Numbers");
            System.out.println("[4] Lowercase, Uppercase, Numbers, & Punctuation");
            System.out.println("[5] Quit");
            System.out.println();
            System.out.print("Enter Selection (1-5): ");
            int selection = in.nextInt();
     
            if (selection == 1)
            {
                while (randNum >= 97 && randNum <= 122)
                {
                    randNum = randNumList.nextInt(122);
                }
            }
            else if (selection == 2)
            {
                while (!(randNum >= 65 && randNum <= 90) && !(randNum >= 97 && randNum >=122))
                {
                    randNum = randNumList.nextInt(122);
                }
            }
            else if (selection == 3)
            {
                while (!(randNum >= 60 && randNum <= 71) && !(randNum >= 65 && randNum <= 90) && !(randNum >= 97 && randNum >=122))
                {
                    randNum = randNumList.nextInt(122);
                }
            }
            else if (selection == 4)
            {
                while (randNum >= 48)
                {
                    randNum = randNumList.nextInt(127);
                }
            }
            else
            {
                System.exit(0);
            }
     
            System.out.print("Password Length (1-14): ");
            int passLength = in.nextInt();
     
            System.out.println("Password: " + password);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,830
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Password Generating Program Help

    How do I code the part where it generates the specific length of the password?
    Can you count the number of characters added to build the password and stop when the desired length is reached?


    Where is anything put into the password variable?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Password Generating Program Help

    Quote Originally Posted by Norm View Post
    Can you count the number of characters added to build the password and stop when the desired length is reached?
    Something like this?

    if (selection == 1)
       {
            while (randNum >= 97 && randNum <= 122)
            {
                for (int loop = 0; loop <= passLength; loop++)
                {
                    randNum = randNumList.nextInt(122);
                    password += in.nextInt();
                }
            }
       }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,830
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Password Generating Program Help

    What happens when you execute that code? What is printed out at the end for the value of password?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Password Generating Program Help

    Quote Originally Posted by Norm View Post
    What happens when you execute that code? What is printed out at the end for the value of password?
    It's blank. I must have made an error somewhere. I don't really know where though.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,830
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Password Generating Program Help

    Can you explain how the code you posted in post#3 is supposed to work?
    Copy the code here and add a comment to each statement describing what it is supposed to do.
    Explain how and when each variable gets it value and how that value is used and changed.
    For example, explain what randNum's value is when this is executed
     while (randNum >= 97 && randNum <= 122)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Password Generating Program Help

    Quote Originally Posted by Norm View Post
    Can you explain how the code you posted in post#3 is supposed to work?
    Copy the code here and add a comment to each statement describing what it is supposed to do.
    Explain how and when each variable gets it value and how that value is used and changed.
    For example, explain what randNum's value is when this is executed
     while (randNum >= 97 && randNum <= 122)
    I meant to put randNum where it originally said in.nextInt();

    if (selection == 1) // If they choose Menu Option 1, then this code below will execute.
       {
            while (randNum >= 97 && randNum <= 122) // When randNum is generated, if it is >= 97 but <= 122, then it goes to the for loop.
            {
                for (int loop = 0; loop <= passLength; loop++) // I ask the user to enter in the character length and declare it as int passLength. The loop goes through however many times they put in for the password length.
                {
                    randNum = randNumList.nextInt(122); // It gets the next number between 97 and 122.
                    password += randNum; // It adds the number to the String password.
                }
            }
       }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,830
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Password Generating Program Help

    What is the value of randNum when the while statement is first executed?

    randNum = randNumList.nextInt(122); // It gets the next number between 97 and 122
    Read the API doc to see what that method returns.

    password += randNum; // It adds the number to the String password.
    Write a small simple test program with a String and a value for randNum, do the above statement and print out the value of password to see what it is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can Someone please help me with my password program?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2012, 12:16 PM
  2. [SOLVED] Help Generating a level
    By Montario in forum AWT / Java Swing
    Replies: 22
    Last Post: April 12th, 2012, 08:22 AM
  3. Check Password Program
    By m2msucks in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 6th, 2011, 01:39 AM
  4. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 04:59 PM
  5. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 08:03 AM