import java.util.Scanner;
import java.util.Random;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

public class Password
{
    public static void main(String [] args)
    {
        Scanner in;  
        in = new Scanner(System.in);
        Random rand = 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 = rand.nextInt(26) + 97;
                       password += (char) randNum;
                    }
          else if(choice == 2)
          {
              randNum = rand.nextInt(123);
              while((randNum >= 97 && randNum <= 122) || (randNum >= 65 && randNum <= 90))
              randNum = rand.nextInt(123);
               password += (char) randNum;
            }
         else if(choice == 3)
         {
             randNum = rand.nextInt(123);
             while(randNum < 65 || randNum > 90 && randNum < 97 ||(  randNum < 48 && randNum > 57)  )
               randNum = rand.nextInt(123);
               password += (char) randNum;
            }
            else if(choice == 4)
            {
                randNum = rand.nextInt(123);
                 while(randNum < 65 || (randNum > 90 && randNum < 97) || randNum < 48 && randNum > 57 || (randNum < 33 && randNum > 47) || (randNum < 91 && randNum > 96))
               randNum = rand.nextInt(123);
               password += (char) randNum;
            }
    }
    System.out.println("Password: " + password);
    }
}

This is for my cs class, in the third choice i need to generate a password that has numebrs, lowercase, and upper case, characters, i cant get it to generate numbers, how would i do this.

hope the following code helps,

import java.util.Scanner;
import java.util.Random;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
 
public class PasswordTest
{
    public static void main(String [] args)
    {
        Scanner in;  
        in = new Scanner(System.in);
        Random rand = 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 = rand.nextInt(26) + 97;
                       password += (char) randNum;
                    }
          else if(choice == 2)
          {
              randNum = rand.nextInt(123);
              while(true){
				  if((randNum >= 97 && randNum <= 122) || (randNum >= 65 && randNum <= 90)){
					password += (char) randNum;
					break;
				  }
				  else{
		              randNum = rand.nextInt(123);
				  }
			  }
            }
         else if(choice == 3)
         {
             randNum = rand.nextInt(123);
			 while(true){
				if( (randNum >= 65 && randNum <= 90) || (randNum >= 97 && randNum <= 122) || (  randNum >= 48 && randNum <= 57)  ){
					password += (char) randNum;
					break;
				}
				else{
					randNum = rand.nextInt(123);
				}
			 }
            }
            else if(choice == 4)
            {
                randNum = rand.nextInt(123);
                while(true){
				if( (randNum >= 65 && randNum <= 90) || (randNum >= 97 && randNum <= 122) || (  randNum >= 48 && randNum <= 57) || (  randNum >= 33 && randNum <= 47) ){
					password += (char) randNum;
					break;
				}
				else{
					randNum = rand.nextInt(123);
				}
			  }
            }
    }
    System.out.println("Password: " + password);
    }
}
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.