How to Create a Java Program for Generating Custom Passwords?

In summary: } } //end if //end for //end while //end if //end while
  • #1
Cia
17
0

Homework Statement


Code:
import java.util.Scanner;
import java.util.Random;

public class Passwords
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Random r = 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, and Numbers                   *");
        System.out.println("*  [4] Lowercase, Uppercase, Numbers, and Punctuation      *");
        System.out.println("*  [5] Quit                                                *");
        System.out.println("******************************************************************");
        System.out.println("Enter Selection (1-5): ");
        int choice = in.nextInt();
        System.out.println("Password Length (1-14): ");
        int passwordLength = in.nextInt();
        int randNum = 0;
       
       
        String password =  "";
       
       
         
        for (int counter = 0; counter <  passwordLength; counter++)
        {
           
           

            
            
                 if (choice ==1)
                 {
                   randNum =  97 + r.nextInt (26) ;
                  while ( !( (randNum>=97) && (randNum<=122) ))
                  {
                       randNum = 97 + r.nextInt (26) ; //generate a new random value
                       //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                      
                    }//end while
               
                   //concatenate new char to the password since the value is in the desired range
                   char letter = (char) randNum;
                       password += letter;
            
                    }
                  
               
               
                    else if (choice == 2)
                   {
                        randNum = 65 + r.nextInt (58) ;
                      while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
                     
                      {
                          randNum = 65 + r.nextInt (58) ;
                          //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                        
                       
                        }
                        char letter = (char) randNum;
                          password += letter;
               
               
                     
                    }
                  
           
                       else if (choice ==3)
                          {
                             randNum =  48 + r.nextInt (79) ;
                           while (!(randNum>=65) && (randNum<=90)||(randNum>=97) && (randNum<=122) || (randNum>=48) && (randNum<=57))
                           {
                              randNum =  48 + r.nextInt (79);
                             //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                              
                               
                           
                           
                            }

                           
                            char letter = (char) randNum;
                               password += letter;
               
                            
          
                           }
                       
                        else if (choice ==4)
                              {
                                 randNum =  48 + r.nextInt (78) ;
                               while( !(randNum>=58) && (randNum<=64)&&(randNum>=48) && (randNum<=67)&&(randNum>=65) && (randNum<=90) &&(randNum>=97) && (randNum<=122))
                     
                               {
                                randNum =  48 + r.nextInt (78) ;
                                  //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                              
                                }
                                char letter = (char) randNum;
                               password += letter;
               
                           
                             
          
                              }
                                   else
                                  {
                                   System.out.print("Good Bye!");

                                  }
                   
                   
        
               
                
         }//end of for loop
    
    System.out.println( "" + "" + password );
            
    }//end of main method
}//end of class

Homework Equations

The Attempt at a Solution

 
Physics news on Phys.org
  • #2
Cia said:

Homework Statement


Code:
import java.util.Scanner;
import java.util.Random;

public class Passwords
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Random r = 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, and Numbers                   *");
        System.out.println("*  [4] Lowercase, Uppercase, Numbers, and Punctuation      *");
        System.out.println("*  [5] Quit                                                *");
        System.out.println("******************************************************************");
        System.out.println("Enter Selection (1-5): ");
        int choice = in.nextInt();
        System.out.println("Password Length (1-14): ");
        int passwordLength = in.nextInt();
        int randNum = 0;
      
      
        String password =  "";
      
      
        
        for (int counter = 0; counter <  passwordLength; counter++)
        {
          
          

           
           
                 if (choice ==1)
                 {
                   randNum =  97 + r.nextInt (26) ;
                  while ( !( (randNum>=97) && (randNum<=122) ))
                  {
                       randNum = 97 + r.nextInt (26) ; //generate a new random value
                       //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                     
                    }//end while
              
                   //concatenate new char to the password since the value is in the desired range
                   char letter = (char) randNum;
                       password += letter;
           
                    }
                 
              
              
                    else if (choice == 2)
                   {
                        randNum = 65 + r.nextInt (58) ;
                      while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
                    
                      {
                          randNum = 65 + r.nextInt (58) ;
                          //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                       
                      
                        }
                        char letter = (char) randNum;
                          password += letter;
              
              
                    
                    }
                 
          
                       else if (choice ==3)
                          {
                             randNum =  48 + r.nextInt (79) ;
                           while (!(randNum>=65) && (randNum<=90)||(randNum>=97) && (randNum<=122) || (randNum>=48) && (randNum<=57))
                           {
                              randNum =  48 + r.nextInt (79);
                             //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                             
                              
                          
                          
                            }

                          
                            char letter = (char) randNum;
                               password += letter;
              
                           
         
                           }
                      
                        else if (choice ==4)
                              {
                                 randNum =  48 + r.nextInt (78) ;
                               while( !(randNum>=58) && (randNum<=64)&&(randNum>=48) && (randNum<=67)&&(randNum>=65) && (randNum<=90) &&(randNum>=97) && (randNum<=122))
                    
                               {
                                randNum =  48 + r.nextInt (78) ;
                                  //randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
                             
                                }
                                char letter = (char) randNum;
                               password += letter;
              
                          
                            
         
                              }
                                   else
                                  {
                                   System.out.print("Good Bye!");

                                  }
                  
                  
       
              
               
         }//end of for loop
   
    System.out.println( "" + "" + password );
           
    }//end of main method
}//end of class

Homework Equations

The Attempt at a Solution

What's your question? How are you stuck?
 
  • #3
When, i select a selection sometimes I get symbols when am not suppose to. Selection 1 is fine but sometimes selection 2-4 are off.
 
  • #4
You have lines similar to this for choices 2, 3, and 4.
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
The condition above evaluates to true if
1) randNum < 65 AND randNum <= 90,
OR
2) randNum >= 97 AND randNum <=122
Do you see why this is happening? This looks like a copy and paste problem that you didn't think hard enough about.
 
  • #5
Mark44 said:
You have lines similar to this for choices 2, 3, and 4.
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
The condition above evaluates to true if
1) randNum < 65 AND randNum <= 90,
OR
2) randNum >= 97 AND randNum <=122
Do you see why this is happening? This looks like a copy and paste problem that you didn't think hard enough about.
Actually, i did think. Its just a java basic class am in now. So, basically you're saying this is happening because of the or symbol? I even tried to use the and symbol but, it still did not come out correctly though
 
  • #6
The problem in that line is what the negation "!" is being applied to. By contrast for choice ==1, when you don't really need the while loop at all, you are using it more correctly.
 
  • #7
Joffan said:
The problem in that line is what the negation "!" is being applied to. By contrast for choice ==1, when you don't really need the while loop at all, you are using it more correctly.
Okay, my teacher wants me to use the while loop. So, should I just take the negation symbol out?
 
  • #8
If you don't understand what the negation is doing, you need to find out. The while loop is useful - if the logical expression is correctly written - for the other 3 choices. Again, if you don't understand why, you need to find out and understand what it is doing.

It might be useful all round if you were to describe in English what you are trying to do in, say, the choice==2 section.
 
  • #9
Joffan said:
If you don't understand what the negation is doing, you need to find out. The while loop is useful - if the logical expression is correctly written - for the other 3 choices. Again, if you don't understand why, you need to find out and understand what it is doing.

It might be useful all round if you were to describe in English what you are trying to do in, say, the choice==2 section.
I know that the negation means "not". Choice 2 and choice 3 give me symbols when they are not suppose to.
 
  • #10
Cia said:
I know that the negation means "not". Choice 2 and choice 3 give me symbols when they are not suppose to.
Yes, they will - that is a bug - but can you describe what the intended purpose of the test in the choice==2 while statement is? And remember that the while loop executes as long as that test evaluates to true.
 
  • #11
Cia said:
I know that the negation means "not". Choice 2 and choice 3 give me symbols when they are not suppose to.
In post #4 I described what makes the conditions true when for choice is 2, 3, or 4. Is that the logic you intended?
 
  • #12
Joffan said:
Yes, they will - that is a bug - but can you describe what the intended purpose of the test in the choice==2 while statement is? And remember that the while loop executes as long as that test evaluates to true.
 
  • #13
So with that begin said I should remove the negation.
 
  • #14
Cia said:
So with that being said I should remove the negation.
No. You should clearly express what it is you want the test to do, and change it so that it actually does it.

Removing the negation is a random action, not a debugging step. It will not fix the problem.

It really is important and valuable to take the time to write down exactly what you want that test to achieve.
 
  • #15
Joffan said:
No. You should clearly express what it is you want the test to do, and change it so that it actually does it.

Removing the negation is a random action, not a debugging step. It will not fix the problem.

It really is important and valuable to take the time to write down exactly what you want that test to achieve.
Well, I know that i want choice 2 to give me lower and upper case letters only. And I know that i want choice 3 to give me lower, uppercase letters and numbers.
 
  • #16
Cia said:
Well, I know that i want choice 2 to give me lower and upper case letters only. And I know that i want choice 3 to give me lower, uppercase letters and numbers.
Right. So looking at the while loop for choice 2, what is the test doing to achieve that end?
 
  • #17
Cia said:
Well, I know that i want choice 2 to give me lower and upper case letters only. And I know that i want choice 3 to give me lower, uppercase letters and numbers.

Considering Joffan's question, how should the condition in the while loop be changed?
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))

Note that you are also going to have to change the logic in the loops for choice == 3 and choice == 4.
 
  • #18
Mark44 said:
Considering Joffan's question, how should the condition in the while loop be changed?
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))

Note that you are also going to have to change the logic in the loops for choice == 3 and choice == 4.
I should probably take the negation out and use the and symbol instead.
 
  • #19
Cia said:
I should probably take the negation out and use the and symbol instead.

:) You won't solve this by guessing at changes.

As a step along the way to specifying the purpose - what do the numbers in that test represent? Describe each of those four comparisons to me.
 
  • #20
Joffan said:
:) You won't solve this by guessing at changes.

As a step along the way to specifying the purpose - what do the numbers in that test represent? Describe each of those four comparisons to me.
I actually tried it and it did not work. The numbers represent letters in the ASII table. Actually this is the assignment constructions
  1. Display a menu giving the user a choice of character sets to use to construct the password. (Note: Do not use the first range of punctuation symbols with ASCII values from 33-47).
  2. Allow the user to select the number of characters in the password (at least up to 14).
  3. Create a randomly generated password from the character sets selected by the user.
 
  • #21
Joffan said:
As a step along the way to specifying the purpose - what do the numbers in that test represent? Describe each of those four comparisons to me.
Cia said:
I actually tried it and it did not work. The numbers represent letters in the ASII table.
Tried what? Joffan is asking you to explain what each of the four comparisons in this code:
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
In other words, what values of randNum make the first comparison true? Same question for the second comparison. Same question for the third comparison. Same question for the fourth comparison.

Below, yes we get that. Try to focus on what we're asking.
Cia said:
Actually this is the assignment constructions
  1. Display a menu giving the user a choice of character sets to use to construct the password. (Note: Do not use the first range of punctuation symbols with ASCII values from 33-47).
  2. Allow the user to select the number of characters in the password (at least up to 14).
  3. Create a randomly generated password from the character sets selected by the user.
 
  • #22
Mark44 said:
Tried what? Joffan is asking you to explain what each of the four comparisons in this code:
Code:
while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
In other words, what values of randNum make the first comparison true? Same question for the second comparison. Same question for the third comparison. Same question for the fourth comparison.

Below, yes we get that. Try to focus on what we're asking.
the randNum for choice 1 is 26. I get this by subtracting 122 from 97 and adding 1. With choice 2, the randNum is 58, 122- 65 +1.
 
  • #23
Cia said:
the randNum for choice 1 is 26. I get this by subtracting 122 from 97 and adding 1. With choice 2, the randNum is 58, 122- 65 +1.
?
No, randNum is a random number between 65 and 123. That's what your code at the top of the if block is doing. nextInt returns a random number in the range 0...58 in this case, because you're calling nextInt with an argument of 58.

So randNum could be any number in the set {65, 66, 67, 68, ..., 121, 122, 123}
Can you use every number in this set? Not every number in this set is the ASCII code for an uppercase letter or lowercase letter. The four conditions below are supposed to pick out the numbers you can use.
Code:
else if (choice == 2)
{
     randNum = 65 + r.nextInt (58) ;
     while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
    {
          ...
    }
}
 
  • #24
Mark44 said:
?
No, randNum is a random number between 65 and 123. That's what your code at the top of the if block is doing. nextInt returns a random number in the range 0...58 in this case, because you're calling nextInt with an argument of 58.

So randNum could be any number in the set {65, 66, 67, 68, ..., 121, 122, 123}
Can you use every number in this set? Not every number in this set is the ASCII code for an uppercase letter or lowercase letter. The four conditions below are supposed to pick out the numbers you can use.
Code:
else if (choice == 2)
{
     randNum = 65 + r.nextInt (58) ;
     while (!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122))
    {
          ...
    }
}
I have a question: Should I place randNum = 65 + r.nextInt (58) ; after the while loop? Also, should I change my ranges?
p.s. thanks for being patience with me so far
 
  • #25
Let's deal with the conditions first. What has to happen so that randNum's value corresponds to an uppercase letter or a lowercase letter? The password string that you build up should contain only these characters.
 
  • #26
Mark44 said:
Let's deal with the conditions first. What has to happen so that randNum's value corresponds to an uppercase letter or a lowercase letter? The password string that you build up should contain only these characters.
The randNum value must be in the two ranges
 
  • #27
Cia said:
The randNum value must be in the two ranges
It can't be in the two ranges - it can only be in the first range or the second range, or neither.

!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122)

Each of the four comparisons above will evaluate to true or false. To help you understand what your code is doing, please state what each comparison evaluates to for each of the following numbers.

Code:
Number          Comparison 1(T/F)     Comparison 2(T/F)   Comparison 3(T/F)    Comparison 4(T/F)   
66
93
101
123
 
  • #28
Here's what it seems is intended to happen in each choice block, descriptively:
Java:
          get a random number that can represent any valid character (but maybe also invalid characters)
          while (the number does NOT represent a valid character)
          {
              refresh the random number in the same way as above
          }
          // here we have left the while loop, so the number must now represent a valid character
          turn the number into a character
          append the character onto the password

What we are trying to help you do is learn to write that 'while' test properly.
 
  • #29
Mark44 said:
It can't be in the two ranges - it can only be in the first range or the second range, or neither.

!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122)

Each of the four comparisons above will evaluate to true or false. To help you understand what your code is doing, please state what each comparison evaluates to for each of the following numbers.

Code:
Number          Comparison 1(T/F)     Comparison 2(T/F)   Comparison 3(T/F)    Comparison 4(T/F)  
66                        f
93                        t
101                     f
123                     t
 
  • #30
@Cia, where are the other entries in the table? It looks like you filled out only the column for the first comparison.
 
  • #31
Not the solution to the problem, but no one commented yet that, unless you are generating huge passwords (and in that case Java is by far not the best choice), you should use the Character.isLowerCase() and Character.isUpperCase() methods. It is considered superior due to a series of reasons (some somewhat stupid, tagged as 'enterprisey') that are sold by the name 'Software Engineering'.
 
  • #32
mafagafo said:
Not the solution to the problem, but no one commented yet that, unless you are generating huge passwords (and in that case Java is by far not the best choice), you should use the Character.isLowerCase() and Character.isUpperCase() methods. It is considered superior due to a series of reasons (some somewhat stupid, tagged as 'enterprisey') that are sold by the name 'Software Engineering'.
Mark44 said:
@Cia, where are the other entries in the table? It looks like you filled out only the column for the first comparison.
Mark44 said:
It can't be in the two ranges - it can only be in the first range or the second range, or neither.

!(randNum>=65) && (randNum<=90) || (randNum>=97) && (randNum<=122)

Each of the four comparisons above will evaluate to true or false. To help you understand what your code is doing, please state what each comparison evaluates to for each of the following numbers.

Code:
Number          Comparison 1(T/F)     Comparison 2(T/F)   Comparison 3(T/F)    Comparison 4(T/F)
66                        f                    f                   f                     f
93                        t                    t                   t                     t
101                      f                    f                   f                     f
123                      t                    t                   t                     t
 
Last edited by a moderator:
  • #33
Did you even try?
 
  • #34
mafagafo said:
Did you even try?
that is what the ranges should be because "!" means not. Am saying if a number is not in the range for example 66 is our number and the range id !(randNum>=65)
since 66 is greater than or equal to 65 it statement would be false
 
  • #35
Are you up to try the two methods I suggested earlier? When do you want to use the int? When it is lower OR upper, right? Implement it that way.
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top