Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,274 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,160 people online right now. Registration is fast and FREE... Join Now!




Random Password Generator (Logical Error?)

 
Reply to this topicStart new topic

Random Password Generator (Logical Error?), Topics merged... Please avoid double postings

jtb1217
23 Dec, 2008 - 12:58 AM
Post #1

New D.I.C Head
*

Joined: 22 Dec, 2008
Posts: 4

I had to write a program that generated a random password given choices about characters and length (you can see the options in the menu that's in the code below).

The program will work perfectly if you choose option #4 (because it includes all the possible characters). The others tend to get stuck in an infinite loop somewhere. Where is the error in my code logic.

Any help would be greatly appreciated. Thanks


CODE

import java.util.Scanner;
import java.util.Random;

class Password
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner (System.in);
        Random randNumList = new Random ();
        
        System.out.println("                    Password Generation Menu                    ");
        System.out.println("****************************************************************");
        System.out.println("*\n[1] Lowercase Letters                                       *");
        System.out.println("*\n[2] Lowercase & Uppercase Letters                           *");
        System.out.println("*\n[3] Lowercase, Uppercase, & Numbers                         *");
        System.out.println("*\n[4] Lowercase, Uppercase, Numbers, and Punctuation          *");
        System.out.println("*\n[5] Quit                                                    *");
        System.out.println("****************************************************************");
        
        System.out.print("Enter Selection (1-5): ");
        String selection = in.next();
        
        System.out.println("");
        System.out.print("Password Length (1-14): ");
        int passLength = in.nextInt();
        
        
        for (int i = 1; i <= passLength; i++)
        {
            int charNum = (int)(randNumList.nextDouble()*(126-48) + 48);
            Boolean condState;    
        
            if(selection.equals("1"))
            {
                 condState = (charNum <= 122 && charNum >= 97);
            }
                else if(selection.equals("2"))
                {
                    condState = ((charNum <=122 && charNum >= 97) || (charNum <= 90 && charNum >= 65));
                }
                    else if(selection.equals("3"))
                    {
                        condState = ((charNum <=122 && charNum >= 97) || (charNum <= 90 && charNum >= 65) || (charNum <= 57 && charNum >= 48));                
                    }    
                        else if(selection.equals("4"))
                        {
                            condState = (charNum <= 126 && charNum >= 48);
                        }
                            else
                            {
                               condState = charNum == 0;
                               System.out.println("Program Terminated");
                                System.exit(0);
                            }
                            
            while(!condState)
            {
                charNum = (int)(randNumList.nextDouble()*(126-48) + 48);
            }    
            
          System.out.print((char)(charNum));
                
        }
    }
}    


User is offlineProfile CardPM
+Quote Post


DillonSalsman
RE: Random Password Generator (Logical Error?)
23 Dec, 2008 - 01:49 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 88



Thanked: 1 times
Dream Kudos: 50
My Contributions
Hmm.. well.
I think a switch-case would be more efficient than all of your else if statements, as well as a bit easier to read.
Also, try creating a method newPassword(int length, int type, for example:
CODE

public String newPassword(int length, int type)
{
switch (type)
{
    case 0: System.out.println("You don't want a password."); break;
    case 1: //generate password via choice one's standards
    break;
    case 2: //etc.
    break;
    case 3: //and so forth
    break;
    default: System.out.println("You didn't choose a defined password format."); break;
    
}
}

Sorry, its 4 am here.

This post has been edited by DillonSalsman: 23 Dec, 2008 - 01:51 AM
User is offlineProfile CardPM
+Quote Post

jtb1217
RE: Random Password Generator (Logical Error?)
28 Dec, 2008 - 06:08 PM
Post #3

New D.I.C Head
*

Joined: 22 Dec, 2008
Posts: 4

This code generates a password of ones chosen length and including chosen character types (see the menu below).

All the cases in the switch statement work perfectly EXCEPT FOR CASE 1! I can't figure it out. Sometimes it will output one or two letters but it always gets stuck in an infinite loop. Like I said, I dont have this trouble with the other cases. Is there some error in my logic?



CODE

import java.util.Scanner;
import java.util.Random;

class Password2
{
    public static void main(String [] args)
    {

        Scanner in = new Scanner (System.in);
        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, and Punctuation         *");
        System.out.println("*   [5] Quit                                                   *");
        System.out.println("****************************************************************");
        
        System.out.print("Enter Selection (1-5): ");
        String selection = in.next();
        int selInt = Integer.parseInt(selection);
              
        int charNum = 0;
        System.out.print("\nPassword Length (1-14): ");
        int passLength = in.nextInt();
        
          
        
            switch(selInt)
            {
                case 1:
                    for(int i =1; i <= passLength; i++)
                    {  
                        charNum = (randNumList.nextInt(79) + 48);
                        while(!(charNum >= 97 && charNum <=122));
                        {
                            charNum = (randNumList.nextInt(79) + 48);
                        }
                        System.out.print((char)charNum);
                    }
                    break;
                    
                case 2:
                    for(int i =1; i <= passLength; i++)
                    {  
                        charNum = (randNumList.nextInt(79) + 48);
                        while(!((charNum >= 97 && charNum <=122) || (charNum >= 65 && charNum <= 90)))
                        {
                            charNum = (randNumList.nextInt(79) + 48);
                        }
                        System.out.print((char)charNum);
                    }
                    break;
                    
                case 3:
                    for(int i =1; i <= passLength; i++)
                    {  
                        charNum = (randNumList.nextInt(79) + 48);
                        while(!((charNum >= 97 && charNum <=122) || (charNum >= 65 && charNum <= 90) || (charNum >= 48 && charNum <= 57)))
                        {
                            charNum = (randNumList.nextInt(79) + 48);
                        }
                        System.out.print((char)charNum);
                    }                
                    break;
                    
                case 4:
                    for(int i =1; i <= passLength; i++)
                    {  
                        charNum = (randNumList.nextInt(79) + 48);
                        while(!(charNum >= 48 && charNum <= 126))
                        {
                            charNum = (randNumList.nextInt(79) + 48);
                        }
                        System.out.print((char)charNum);
                    }    
                    break;
                    
                default:
                    System.out.print("Progran Terminated!");
                    System.exit(0);
            }      


                    
     }

}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 01:34PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month