1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Password generator in Java

  1. Nov 19, 2014 #1

    Cia

    User Avatar

    1. The problem statement, all variables and given/known data
    Code (Text):
    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
    2. Relevant equations


    3. The attempt at a solution
     
  2. jcsd
  3. Nov 19, 2014 #2

    Mark44

    Staff: Mentor

    What's your question? How are you stuck?
     
  4. Nov 19, 2014 #3

    Cia

    User Avatar

    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.
     
  5. Nov 19, 2014 #4

    Mark44

    Staff: Mentor

    You have lines similar to this for choices 2, 3, and 4.
    Code (Text):

    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.
     
  6. Nov 19, 2014 #5

    Cia

    User Avatar

    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
     
  7. Nov 19, 2014 #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.
     
  8. Nov 19, 2014 #7

    Cia

    User Avatar

    Okay, my teacher wants me to use the while loop. So, should I just take the negation symbol out?
     
  9. Nov 19, 2014 #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.
     
  10. Nov 20, 2014 #9

    Cia

    User Avatar

    I know that the negation means "not". Choice 2 and choice 3 give me symbols when they are not suppose to.
     
  11. Nov 20, 2014 #10
    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.
     
  12. Nov 20, 2014 #11

    Mark44

    Staff: Mentor

    In post #4 I described what makes the conditions true when for choice is 2, 3, or 4. Is that the logic you intended?
     
  13. Nov 20, 2014 #12

    Cia

    User Avatar

     
  14. Nov 20, 2014 #13

    Cia

    User Avatar

    So with that begin said I should remove the negation.
     
  15. Nov 20, 2014 #14
    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.
     
  16. Nov 20, 2014 #15

    Cia

    User Avatar

    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.
     
  17. Nov 20, 2014 #16
    Right. So looking at the while loop for choice 2, what is the test doing to achieve that end?
     
  18. Nov 20, 2014 #17

    Mark44

    Staff: Mentor

    Considering Joffan's question, how should the condition in the while loop be changed?
    Code (Text):
    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.
     
  19. Nov 20, 2014 #18

    Cia

    User Avatar

    I should probably take the negation out and use the and symbol instead.
     
  20. Nov 20, 2014 #19
    :) 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.
     
  21. Nov 20, 2014 #20

    Cia

    User Avatar

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Password generator in Java
  1. Java prob. (Replies: 1)

  2. HeapSort Java (Replies: 3)

Loading...