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