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?
Code :
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);
}
}
Re: Password Generating Program Help
Quote:
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?
Re: Password Generating Program Help
Quote:
Originally Posted by
Norm
Can you count the number of characters added to build the password and stop when the desired length is reached?
Something like this?
Code :
if (selection == 1)
{
while (randNum >= 97 && randNum <= 122)
{
for (int loop = 0; loop <= passLength; loop++)
{
randNum = randNumList.nextInt(122);
password += in.nextInt();
}
}
}
Re: Password Generating Program Help
What happens when you execute that code? What is printed out at the end for the value of password?
Re: Password Generating Program Help
Quote:
Originally Posted by
Norm
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.
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
Code :
while (randNum >= 97 && randNum <= 122)
Re: Password Generating Program Help
Quote:
Originally Posted by
Norm
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
Code :
while (randNum >= 97 && randNum <= 122)
I meant to put randNum where it originally said in.nextInt();
Code :
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.
}
}
}
Re: Password Generating Program Help
What is the value of randNum when the while statement is first executed?
Quote:
randNum = randNumList.nextInt(122); // It gets the next number between 97 and 122
Read the API doc to see what that method returns.
Quote:
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.