My assignment is to randomly generate a password based on the selection chosen by the user. So far, my code is unfinished but that's because I've run into a problem.

For the second selection(Lowercase and uppercase letters). My password will display lowercase and uppercase letters but it won't be enough letters (I also prompt the user to type in how long they want their password to be).

For example, if the person wants a password with a length of 14 and lowercase and uppercase letters, I'll only get a password that's less than that. It's never the length that it's supposed to be. I know this is caused by a few lines of code. More specifically, the lines under "else if(userSelection == 2)". So if someone could help me with that that'd be great.

import java.util.Scanner;
import java.util.Random;
public class Password
{
    public static void main(String [] args)
    {
    Scanner in = new Scanner(System.in);
    int userSelection;
    int passwordLength = 0;
    Random randNum = new Random();
    int randNumAscii = 0;
    String generatedPassword = "";

    System.out.println("\t\t\tPassword Generation Menu");
    System.out.println("********************************************************");
    System.out.println("[1] Lowercase Letters");
    System.out.println("[2] Lowercase & Uppercase Letters");
    System.out.println("[3] Lowercase, Uppercase, and Numbers");
    System.out.println("[4] Lowercase, Uppercase, Numbers, and Punctuation");
    System.out.println("[5] Quit");
    System.out.println("********************************************************");

    System.out.print("\nEnter Selection (1-5): ");
    userSelection = in.nextInt();

    if(userSelection == 5)
        System.exit(0);

    System.out.print("Password Length (1-14): ");
    passwordLength = in.nextInt();

    for(int count = 0; count < passwordLength; count++){    
        if(userSelection == 1){
            randNumAscii = randNum.nextInt(26) + 97;
            generatedPassword += (char)randNumAscii;
        }

        else if(userSelection == 2){
            randNumAscii = randNum.nextInt(123);
                if(randNumAscii >= 65 && randNumAscii <= 90 || randNumAscii >= 97 && randNumAscii <= 122)
                    generatedPassword += (char)randNumAscii;
                else
                    randNumAscii = randNum.nextInt(123); 
                }     
    }
            System.out.println(generatedPassword);
}
}

I think it would be easier if you'd use an array or an arraylist to have a dynamic length and store a value for each index for the password

I am not exactly sure what you are trying to accomplish with this line of code

randNumAscii = randNum.nextInt(123);

in your final else statement, but I assume you are just trying to get the program to realize that it is out of the correct range and move on to the next character int the correct range. I was able to make this happen by replacing this line with the following.

count--;
continue;

What this does is when the program hits the else statement becasue it has chosen a number that is out of the available range, it takes the counter back by 1 so that the counter is still equal to the number of characters that have been added to the password, then it just tells the loop to go ahead and continue looping.

Hope this makes sense and I am not totally missing the point of the line of code I removed. Good luck.

The problem is at lines 42-43.

` else
     randNumAscii = randNum.nextInt(123);'

In the above code count gets incremented and nothing is added to generatedPassword.
Hence these lines should be modified as below

` else{
     randNumAscii = randNum.nextInt(123);
     count=count-1;
     }

Below is the sample output run after the fix

    **  Password Generation Menu**

********************************************************
[1] Lowercase Letters
[2] Lowercase & Uppercase Letters
[3] Lowercase, Uppercase, and Numbers
[4] Lowercase, Uppercase, Numbers, and Punctuation
[5] Quit
********************************************************

Enter Selection (1-5): 2
Password Length (1-14): 10
DkVqMvGQM

You can see the output has 10 characters, which is what neeeded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.