alright here are my instructions:

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).
4. Allow the user to select the number of characters in the password (at least 1--14).
5. Create a randomly generated password from the selected character sets.

here is my code:

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);
    
    
        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 = ((int)(0+ Math.random()* 122));
        String password = "";
        randNum = ((int)(0+ Math.random()* 122));
        boolean lowerCase = (randNum>=97) && (randNum<=122);
        boolean upperCase = (randNum>=65) && (randNum<=90);
        boolean numbers = (randNum>=48) && (randNum<=57);
        boolean punctuation = (randNum>=58) && (randNum<=64);
	
        

        
        System.out.println("\n");
        
        
        
       for (int counter = 0; counter <  passwordLength; counter++) 
       {
           
          
           
           if (choice ==1)
           {
            
           while(lowerCase)
           {
               
               password += (char)randNum;
               counter++;

            }
            System.out.println(password);
         
           }
            
            if (choice == 2)
            {
            
            while(lowerCase || upperCase)
            {
                
                password += (char)randNum;
                counter++;
            }
            System.out.println(password);
            }
            if (choice ==3)
            {
             
            while(lowerCase || upperCase || numbers)
            {
                password += (char)randNum;
                counter++;
              }
            System.out.println(password);
             }
             if (choice ==4)
            {
             
            while(lowerCase || upperCase || numbers || punctuation)
            {
                password += (char)randNum;
                counter++;
                
            }
            System.out.println(password);
           }
        
    }
        
    }
}

lol here is my output:

Password Generation Menu
********************************************************
* [1] Lowercase Letters *
* [2] Lowercase & Uppercase Letters *
* [3] Lowercase, Uppercase, and Numbers *
* [4] Lowercase, Uppercase, Numbers, and Punctuation *
* [5] Quit *
********************************************************
Enter Selection (1-5):
2
Password Length (1-14):
4

and my problem is that i kind of put together how i thought it work, so if i need to scrap this code, please tell me, but give me enough info so that i know how to repair. im not the best at java, but i dont see whats going wrong. thanks for help in advance.

i have been tinkering with it. it is still in test mode so it probably has some obvious errors, this is just trying to sort things out.

for (int counter = 0; counter <  passwordLength; counter++) 
       {
           
          
           
           if (choice ==1)
           {
           
               
               password += (char)randNum;
               counter++;
               System.out.println(password);
            }

i dont think i really need the whiles. my only problem is that i cant get my random numbers to generate in the ranges that i need.

This program should really have some functions. You have a few tasks here:

  1. Display the options.
  2. Ask the user for the the number of characters.
  3. Ask the user for the option.
  4. Make sure the person enters legal values for both. If not, give an error message and prompt the user again.
  5. Generate the password based on the option and number of characters.
  6. Display the generated password.

Depending on the option selected, you have different character sets to choose from. I would advise you to write a function like this:

String GenerateRandomPassword (int numChars, int option)

You get numChars from the user. Look at the Character class and you'll see it has a variety of functions that could be helpful or not depending on how you approach the random generation. Look at your lines 30 through 33. The Character class has function very similar to C++'s cctype library (i.e. isDigit). If you have a 9 digit password, you'll need to generate (at least) 9 random numbers. You're only producing one. You should have no character by character print statements. Generate a character. Add it to a String. When you have all characters, return it from the function and print the String in main.

But break up your program. You want to use functions. A function may ask for input and validate it. A different function may generate the password. If you don't know how to use functions yet, presumably your teacher will start teaching them soon. They make life much easier.

alright, i have gotten this code almost functional EXCEPT FOR ONE THING, AND I CANT FIND IT ANYWHERE! i have to create a random number with in two different ranges in order to only get uppercase and lowercase letters. i have googled every search term i can think of and no luck.

alright, i have gotten this code almost functional EXCEPT FOR ONE THING, AND I CANT FIND IT ANYWHERE! i have to create a random number with in two different ranges in order to only get uppercase and lowercase letters. i have googled every search term i can think of and no luck.

Two approaches (you could have more than two approaches, but two are enough). Say you need a random number from 5 to 8 or 12 to 15. That's 8 possible numbers.

Method 1

int randomNumber = 0;

// generate a random number from 0 to 7, put in variable randomNumber

if (randomNumber <= 3)
   // add 5 to randomNumber
else
   // add 8 to randomNumber

Method 2

int randomNumber = 0;

do
{
   // generate a random number from 5 to 15, inclusive, put in randomNumber
}
while (randomNumber > 8 && randomNumber < 12); // illegal values.  Pick again.

At the end of either of these methods, randomNumber holds 5,6,7,8,12,13,14, or 15.

alright. so if i had this:

/**
*
*
*
*
*/
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);
    
    
        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 = "";
        
        boolean lowerCase = (randNum>=97) && (randNum<=122);
        boolean upperCase = (randNum>=65) && (randNum<=90);
        boolean numbers = (randNum>=48) && (randNum<=64);
        boolean punctuation = (randNum>=91) && (randNum<=96);
	
        

        
        System.out.println("\n");
        
        
        
       for (int counter = 0; counter <  passwordLength;) 
       {
           
          counter++;
          
                 if (choice ==1)
                 {
           
              randNum =  (int) (Math.random() * (122 - 97 + 1) ) + 97;
              char letter = (char) randNum;
              password += letter;
               
               
            }
            
           else if (choice == 2)
            {
               randNum =   (int) (Math.random() 
              char letter = (char) randNum;
              password += letter;
                
                
            }
            
            
            else if (choice ==3)
            {
               randNum =   (int) (Math.random()
              char letter = (char) randNum;
              password += letter;
                
              }
           
             
            else if (choice ==4)
            {
               randNum =   (int) (Math.random() 
              char letter = (char) randNum;
              password += letter;
                
                
            }
            else
            {
            System.out.print("Good Bye!");
        }
     
        
    }
    System.out.println(password);
        
    }
}

and more precisely this:

randNum = (int) (Math.random() * (122 - 97 + 1) ) + 97;

how could i implement this?

You're program is potentially on the right track in some ways, but it's also way off track. You need to step back and see the forest for the trees rather than focusing on the details. Your program needs a redesign. Lines 39 - 42 make no sense where they are. They could potentially make sense if they were placed somewhere else and if the variables were actually used anywhere, but you can delete them altogether since where they are, they don't do you any good. Presumably you want to do this:

  1. Generate a random number.
  2. Change that random number into a character.
  3. Decide whether that character is a lower case letter, upper case letter, digit, or punctuation.
  4. If the character type is legal for the option the user selected, add the character to the password.
  5. If not, pick another random number, go through the process, and try again.

If the password should be 8 characters, go through the above list eight times.

You're program is potentially on the right track in some ways, but it's also way off track. You need to step back and see the forest for the trees rather than focusing on the details. Your program needs a redesign. Lines 39 - 42 make no sense where they are. They could potentially make sense if they were placed somewhere else and if the variables were actually used anywhere, but you can delete them altogether since where they are, they don't do you any good. Presumably you want to do this:

  1. Generate a random number.
  2. Change that random number into a character.
  3. Decide whether that character is a lower case letter, upper case letter, digit, or punctuation.
  4. If the character type is legal for the option the user selected, add the character to the password.
  5. If not, pick another random number, go through the process, and try again.

If the password should be 8 characters, go through the above list eight times.

i know those lines are useless, i was keeping them there solely to remind me the ranges of the characters. clearly i know this is my task. i am simply one line away from this being functional and being able to submit. no one seems to understand, if i could figure out a way to get lower and upper case letters from the ascii chart, without the characters in between the two, it would work fine.

It would help if you created a few function that returns either
lowercase, uppercase , number or a punctuations.

The in your randomLowercase(int len) function you can something like
for(int i = 0; i< len; i++ )
randomString += getRandLowerCaseCharacter(); //getRandLowerCaseCharacter returns a char from 'a' to 'z';

Similarly, you can have something like this for numbers and uppercase random string as well.

Then it would be easier to mix up your functions to get lowercase,uppercase and numbers all combined.

i cant really do alot of that since we havent been taught. this program is supposed to be on a basic level. im trying to just get something that is simple and works so i can be done with it.

alright, i have came to the conclusion that i guess what i thought would work wont, so, im trying to figure out the best way to do this.
does this look like a start?

else if (choice == 2)
            {
                while (randNum != 97-122 || randNum != 65-90)
                {
                    randNum = (int) Math.random();   
                   char letter = (char) randNum;
                   password += letter;
               }
           
                
            }

Make use of functions. Try something like this

import java.util.Random;

class Main
{
    static Random rand = new Random();

    static void Print(Object ob){
        System.out.print(ob);
    }
    static void Print() {
        System.out.println();
    }
    static char getLowerCaseCharacter(){
        return (char)('a' + rand.nextInt(26));
    }
    static char getUpperCaseCharacter(){
        return (char)('A' + rand.nextInt(26));
    }
    static String getRandLower(int len){
        String tmp = "";
        for(int i = 0; i < len; i++)
            tmp += getLowerCaseCharacter();
        return tmp;
    }
    static String getRandUpper(int len){
        String tmp = "";
        for(int i = 0; i < len; i++)
            tmp += getUpperCaseCharacter();
        return tmp;
    }
   public static void main(String[] Arg)
    {
       Print(getRandLower(rand.nextInt(10)));
       Print();
       Print(getRandUpper(rand.nextInt(10)));
       Print();
        

    }

}

i know those lines are useless, i was keeping them there solely to remind me the ranges of the characters. clearly i know this is my task. i am simply one line away from this being functional and being able to submit. no one seems to understand, if i could figure out a way to get lower and upper case letters from the ascii chart, without the characters in between the two, it would work fine.

You're not one line away from anything. We see those four lines, which could be useful if and only if they were inside the loop, and which are not used, and we can only assume that you're completely confused. We have no idea what your internal thought process is. How would we know that you know those four lines are useless? If they are there solely to remind you of the ranges, they should be comments, not lines of codes. When you post code, we can only assume that you intend to use it as instructions to the Java compiler.

I explained two ways of getting a random number in two separate ranges without getting a number in between in my earlier post. Replace 5 through 8 and 12 through 15 with 65 through 90 and 97 through 122.

alright, i have came to the conclusion that i guess what i thought would work wont, so, im trying to figure out the best way to do this.
does this look like a start?

else if (choice == 2)
            {
                while (randNum != 97-122 || randNum != 65-90)
                {
                    randNum = (int) Math.random();   
                   char letter = (char) randNum;
                   password += letter;
               }
           
                
            }

http://java.sun.com/javase/6/docs/api/java/lang/Math.html Math.random () returns a double from 0 to 1. You want to use nextInt (int) from the Random class.


http://java.sun.com/javase/6/docs/api/java/util/Random.html

while (randNum != 97-122 || randNum != 65-90)

The dash isn't a range. It's subtraction. You're subtracting 122 from 97 and subtracting 90 from 65. This is a range.

while ((randNum >= 97 && randNum <= 122) || (randNum >= 65 && randNum <= 90))
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.