Can somebody help tell what they think is the matter with this code. It is supposed to be a random password generator. You will see what is the matter with when it runs. Thanks in advance

/**
 * This program generates random passwords based on the user selecting the character set and the length. 
 *  
 * 
 * @author John D. Barry 
 * @date   January 26, 2009
 */
import java.util.Scanner;
import java.util.Random;
public class Password
{
    public static void main(String [] args)
    {
        
        
        Scanner in = new Scanner(System.in);
        
        
        
        
        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]   Quit                                                         * ");
        System.out.println("************************************************************************ ");
        System.out.println();
        System.out.print("Enter your selection:  ");
        int selection = in.nextInt();
        
        System.out.println();

        
        
        
        
        

        
        
        
        
        
        
        
        
        if (selection == 1)
        
        {
            
            
                int randNum = 0;
                Random randNumList = new Random();
                
               while( randNum>=97 && randNum<=122)
               
               randNum = randNumList.nextInt();
               char firstChar = (char)randNum;
               randNum = randNumList.nextInt();
               char secondChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirdChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fifthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char sixthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char seventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eighthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char ninthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char tenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eleventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char twelthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirteenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourteenthChar = (char)randNum;
               

                
               System.out.print("Enter the length of the password: ");
               int length = in.nextInt();
                
                if(length == 1)
                {
                    System.out.println(firstChar);
                }
                else if(length == 2)
                {
                    System.out.println(firstChar + secondChar);
                }
                else if(length == 3)
                {
                    System.out.println(firstChar + secondChar + thirdChar);
                }
                else if(length == 4)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar);
                }
                else if(length == 5)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + fifthChar);
                }
                else if(length == 6)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar);
                }
                else if(length == 7)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar);
                }
                else if(length == 8)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar);
                }
                else if(length == 9)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar);
                }
                else if(length == 10)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar);
                }
                else if(length == 11)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar);
                }
                else if(length == 12)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar);
                }
                else if(length == 13)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar);
                }
                else if(length == 14)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar + fourteenthChar);
                }

                }
            
            
            
           
            
            
            
            
           else if(selection == 2)
           
           {
               int randNum = 0;
               Random randNumList = new Random();
           
               while( (randNum>=65 && randNum<=90) && (randNum>=97 && randNum<=122))
            
               randNum = randNumList.nextInt();
               char firstChar = (char)randNum;
               randNum = randNumList.nextInt();
               char secondChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirdChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fifthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char sixthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char seventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eighthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char ninthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char tenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eleventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char twelthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirteenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourteenthChar = (char)randNum;
               
               System.out.print("Enter the length of the password: ");
               int length = in.nextInt();
                
                if(length == 1)
                {
                    System.out.println(firstChar);
                }
                else if(length == 2)
                {
                    System.out.println(firstChar + secondChar);
                }
                else if(length == 3)
                {
                    System.out.println(firstChar + secondChar + thirdChar);
                }
                else if(length == 4)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar);
                }
                else if(length == 5)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + fifthChar);
                }
                else if(length == 6)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar);
                }
                else if(length == 7)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar);
                }
                else if(length == 8)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar);
                }
                else if(length == 9)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar);
                }
                else if(length == 10)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar);
                }
                else if(length == 11)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar);
                }
                else if(length == 12)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar);
                }
                else if(length == 13)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar);
                }
                else if(length == 14)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar + fourteenthChar);
                }
               
               
            }
            
            else if(selection == 3)
            
            {
               int randNum = 0;
               Random randNumList = new Random();
               
               while( (randNum>=65 && randNum<=90) && (randNum>=97 && randNum<=122) && randNum>=48 && randNum<=57)
            
            
               randNum = randNumList.nextInt();
               char firstChar = (char)randNum;
               randNum = randNumList.nextInt();
               char secondChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirdChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fifthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char sixthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char seventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eighthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char ninthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char tenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char eleventhChar = (char)randNum;
               randNum = randNumList.nextInt();
               char twelthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char thirteenthChar = (char)randNum;
               randNum = randNumList.nextInt();
               char fourteenthChar = (char)randNum;
               
               
               System.out.print("Enter the length of the password: ");
               int length = in.nextInt();
                
                if(length == 1)
                {
                    System.out.println(firstChar);
                }
                else if(length == 2)
                {
                    System.out.println(firstChar + secondChar);
                }
                else if(length == 3)
                {
                    System.out.println(firstChar + secondChar + thirdChar);
                }
                else if(length == 4)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar);
                }
                else if(length == 5)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + fifthChar);
                }
                else if(length == 6)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar);
                }
                else if(length == 7)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar);
                }
                else if(length == 8)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar);
                }
                else if(length == 9)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar);
                }
                else if(length == 10)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar);
                }
                else if(length == 11)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar);
                }
                else if(length == 12)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar);
                }
                else if(length == 13)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar);
                }
                else if(length == 14)
                {
                    System.out.println(firstChar + secondChar + thirdChar + fourthChar + sixthChar + seventhChar + eighthChar + ninthChar + tenthChar + eleventhChar + twelthChar + thirteenthChar + fourteenthChar);
                }
               
               
               
               
            
            
            
        }
        
        else if(selection == 4)
        
        {
            System.out.println("Thanks for trying. Remember to create hard to guess passwords. ");
        }
            
        }
    }

Inside code you always can insert own test formula.
first suspected line

         while (randNum >= 97 && randNum <= 122) {         //a-z
                 randNum = randNumList.nextInt();                
        }
        System.out.println(randNum);

First thing first. Have you been told to generated the password this way or you come up with this solution of converting integer to character? Secondly you use while loop without opening and closing bracklets that cause lot of weird behaviour.

The current solution is to complex and it can be simplified with use of 3 (or 2 if you clever) arrays such as

char[] lowerCase = {'a','b','c',.....};
char[] upperCase = {'A','B','C',....};
int[] numbers = {0, 1, 2, ...};

In doing so you need to only generate number between

  • 0 - 25 for lower case
  • 0 - 51 for mix of lower & upper case
  • 0 - 61 for mix of lower & upper case & numbers

Next, random number generating you know you will generated 14 characters so why not do it easier

int[] randomArray = new int[14];
Random rand = new Random();
for(int i = 0; i < randomArray.length; i++){
    randomArray[i] = rand.nextInt(range+1);
}

In doing so you will populate array of 14 elements with values between zero and range (25, 61 or 61 as mention above). Also consider using random generator with seed value as above Random may return same value for few consequently followed requests. something like Random rand = new Random(12345678);

Depending on user input you will display final result

//user want 4 characters password value stored in passwordLength
for(int i = 0; i < passwordLength; i++){
    if(randomArray[i] >= 0 && randomArray[i] < 26){
        System.out.print(lowerCase[randomArray[i]]);}
    else if(randomArray[i] >= 26 && randomArray[i] < 52){
        System.out.print(upperCase[randomArray[i]-26]);}
    else if(randomArray[i] >= 52 && randomArray[i] < 62){
        System.out.print(numbers[randomArray[i]-52]);}
}

If you implement this correctly you will significantly cut down number of lines of your code...

also, I find your code quite unreadable.
it's easier to read if you change that entire if-structure to a switch:

int test = getChosenNumber();
switch(test){
  case 0: // code to run if 0
              break;
  case 1: // code to run if 1
             break;
  case 2: // code to run if 2
              break;
   ...
   default: // all what has to be done if a value not mentioned above in the switch statement
}

it's easier to read and to maintain than a number of if's, especially if you're planning on using nested if-structures

I can help.
Call me <snipped>.

Mark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.