Reputation: 11
so my online AP Comp Sci teacher isn't responding to me and I'm running into problems with my program. I need to create a random password generator according to user input from this list:
System.out.println("※※※※※※※※※※※※※※※※※※※ | Password Generation Menu | ※※※※※※※※※※※※※※※※※※※");
System.out.println("※|==========================================================================|※");
System.out.println("※| [1] Lowercase Letters |※");
System.out.println("※| [2] Lowercase and Uppercase Letters |※");
System.out.println("※| [3] Lowercase, Uppercase and Numbers |※");
System.out.println("※| [4] Lowercase, Uppercase, Numbers and Symbols |※");
System.out.println("※| [5] Quit |※");
System.out.println("※|==========================================================================|※");
System.out.println("※※※※※※※※※※※※※※※※※※※※※※ | Your selection? | ※※※※※※※※※※※※※※※※※※※※※※※");
So far I'm running into problems
int selection = in.nextInt();
if (selection ==1)
{
System.out.println("How many characters will me used in the password? (1 - 14)");
int chars = in.nextInt();
while ( count <= chars)
{
int password;
password += rand.nextInt((122 - 97) + 1)+ 97;
count++;
}
System.out.println("Password: " + password);
}
else if (selection ==2)
{
System.out.println("How many characters will me used in the password? (1 - 14)");
int chars = in.nextInt();
while ( count <= chars)
{
while( !(randNum>=65 && randNum<=90)&& !(randNum>=97 && randNum<=122))
{
randNum = randNumList.nextInt();
int password;
password += randNum;
}
count++;
}
System.out.println("Password: " + password);
}
else if (selection ==3)
{
System.out.println("How many characters will me used in the password? (1 - 14)");
int chars = in.nextInt();
while ( count <= chars)
{
while( !(randNum>=65 && randNum<=90)&& !(randNum>=97 && randNum<=122)&& !(randNum>=48 && randNum<=57))
{
randNum = randNumList.nextInt();
int password;
password += randNum;
}
count++;
}
System.out.println("Password: " + password);
}
else if (selection ==4)
{
System.out.println("How many characters will me used in the password? (1 - 14)");
int chars = in.nextInt();
while ( count <= chars)
{
int password;
password += rand.nextInt((126 - 35) + 1)+ 35;
count++;
}
System.out.println("Password: " + password);
}
else if (selection ==5)
{}
else
{
System.out.println(" ERROR: " + selection + " is not on the menu ");
}
}
Its saying the variable password cannot be found, is this because of the loop? I'm utterly stumped and frustrated as I'm falling behind on my work
(This is my first time asking on this site sorry if its formatted weird)
Upvotes: 1
Views: 4255
Reputation: 269817
There are few problems.
One is the notion of scope. Each variable has a scope, which defines when the variable can be accessed and how long it stores data.
Each of your options, 1-4, is creating one password with a different method, and each of those methods uses a loop to add characters to a password. So, each method should have a variable to store the password while characters are added. In general, the structure could be something like this:
if (selection == 1) {
System.out.println("How many characters?");
int chars = in.nextInt();
StringBuilder password = new StringBuilder(chars);
for (int count = 0; count < chars; ++count) {
password.append((char) ('A' + rand.nextInt(26)));
}
System.out.println("Your password is: " + password);
} else if (selection == 2) {
...
The key to notice here is that we defined a variable, password
, outside of the loop that adds characters. This means that the value of password
will be maintained across each iteration of the loop.
Once the enclosing block (if (selection == 1) { ...
) is exited, however, the variable password
doesn't exist anymore, and its content is no longer accessible. That means that you can declare another variable named password
inside the block that handles the next option, that is completely unrelated to the password
in the first block. It doesn't even have to be the same type.
That's how scope works: a variable defined in a scope is visible to any scopes nested within, but when the scope is exited, the variable no longer exists.
I am assuming that you are trying to generate passwords with letters, numbers, and symbols—a sequence of characters. There are a few different ways to represent text in Java, but I demonstrated above the most convenient option for this problem, a StringBuilder
. You will notice that I use a cast, (char)
, when I append()
each character. That's because the result of adding integers is an integer. StringBuilder
has many append()
methods that take different data types. If you append a char
, that character is appended. But if you append an int
, a series of characters representing that numeric value in decimal is appended. By casting to a char
, we pick the right version of append()
.
You repeat a lot of code. A better structure would avoid that, by "hoisting" common code out of sub-blocks.
int selection = in.nextInt();
System.out.println("How long?");
int chars = in.nextInt();
StringBuilder password = new StringBuilder(chars);
for (int count = 0; count < chars; ++count) {
if (selection == 1) {
password.append((char) ('A' + rand.nextInt(26));
} else if (selection == 2) {
...
}
}
System.out.println("Your password is: " + password);
This saves you from repeating the prompts, input and output processing, declaration and initialization of the password
variable, etc. It makes your code shorter, and if you find a bug, you are more likely to fix it in one place, rather than in every supported option.
As learn more, you'll find better ways to handle the different options, by factoring the character generation into another object, which you choose once, before entering the loop. That way you don't have to constantly re-evaluate which option was selected.
Upvotes: 2