Question

In Java, I have created a program which can generate passwords. Currently the program can generate...

In Java, I have created a program which can generate passwords. Currently the program can generate a password through a menu system of which characters to use. To complete the project, I need a system which allows the user to generate another password after the first password is created. I also need a method to check whether or not the password is complex by checking if the generated password has 3 or more character types and then displaying whether or not the password generated is complex. Code below.

package passwordgenerator;

import java.security.SecureRandom;
import java.util.Scanner;

/**
*
* @author Ben Ney
*/
public class PasswordGenerator {
private static final SecureRandom random = new SecureRandom();

/** different dictionaries used */
private static final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String ALPHA = "abcdefghijklmnopqrstuvwxyz";
private static final String NUMERIC = "0123456789";
private static final String SPECIAL_CHARS = "!@#$%^&*_=+-/";

public static String generatePassword(int len)
{
Scanner scnr = new Scanner(System.in);

System.out.println("[1] Only Lowercase Letters\n[2] Only Uppercase Letters\n[3] Only Numbers\n[4] Only Special Characters\n[5] Lowercase & Uppercase\n[6] Lowercase & Numbers\n"
+ "[7] Lowercase & Sepcial Characters\n[8] Uppercase & Numbers\n[9] Uppercase & Special Characters\n[10] Numbers & Special Characters\n[11] Lowercase, Uppercase, & Numbers\n"
+ "[12] Lowercase, Uppercase, & Special Characters\n[13] Lowercase, Numbers, & Special Characters\n[14] Uppercase, Numbers, Special Characters\n[15] All Characters\n[16] Exit");
int menuOption = scnr.nextInt();
while (menuOption != 16) {
if (menuOption == 1) {
String dic1 = ALPHA;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic1.length());
result += dic1.charAt(index);
}
return result;
}
else if (menuOption == 2) {
String dic2 = ALPHA_CAPS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic2.length());
result += dic2.charAt(index);
}
return result;
}
else if (menuOption == 3) {
String dic3 = NUMERIC;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic3.length());
result += dic3.charAt(index);
}
return result;
}
else if (menuOption == 4) {
String dic4 = SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic4.length());
result += dic4.charAt(index);
}
return result;
}
else if (menuOption == 5) {
String dic5 = ALPHA+ALPHA_CAPS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic5.length());
result += dic5.charAt(index);
}
return result;
}
else if (menuOption == 6) {
String dic6 = ALPHA+NUMERIC;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic6.length());
result += dic6.charAt(index);
}
return result;
}
else if (menuOption == 7) {
String dic7 = ALPHA+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic7.length());
result += dic7.charAt(index);
}
return result;
}
else if (menuOption == 8) {
String dic8 = ALPHA_CAPS+NUMERIC;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic8.length());
result += dic8.charAt(index);
}
return result;
}
else if (menuOption == 9) {
String dic9 = ALPHA_CAPS+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic9.length());
result += dic9.charAt(index);
}
return result;
}
else if (menuOption == 10) {
String dic10 = NUMERIC+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic10.length());
result += dic10.charAt(index);
}
return result;
}
else if (menuOption == 11) {
String dic11 = ALPHA+ALPHA_CAPS+NUMERIC;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic11.length());
result += dic11.charAt(index);
}
return result;
}
else if (menuOption == 12) {
String dic12 = ALPHA+ALPHA_CAPS+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic12.length());
result += dic12.charAt(index);
}
return result;
}
else if (menuOption == 13) {
String dic13 = ALPHA+NUMERIC+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic13.length());
result += dic13.charAt(index);
}
return result;
}
else if (menuOption == 14) {
String dic14 = ALPHA_CAPS+NUMERIC+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic14.length());
result += dic14.charAt(index);
}
return result;
}
else if (menuOption == 15) {
String dic15 = ALPHA+ALPHA_CAPS+NUMERIC+SPECIAL_CHARS;
String result = "";
  
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic15.length());
result += dic15.charAt(index);
}
return result;
}
else {
System.out.println("Wrong option entered");
}
System.out.println("[1] Only Lowercase Letters\n[2] Only Uppercase Letters\n[3] Only Numbers\n[4] Only Special Characters\n[5] Lowercase & Uppercase\n[6] Lowercase & Numbers\n"
+ "[7] Lowercase & Sepcial Characters\n[8] Uppercase & Numbers\n[9] Uppercase & Special Characters\n[10] Numbers & Special Characters\n[11] Lowercase, Uppercase, & Numbers\n"
+ "[12] Lowercase, Uppercase, & Special Characters\n[13] Lowercase, Numbers, & Special Characters\n[14] Uppercase, Numbers, Special Characters\n[15] All Characters\n[16] Exit");
menuOption = scnr.nextInt();
}
return null;
  

  
  
}

  
  


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int length;
//Prompts user to enter length of passoword
System.out.print("Enter the length of the password: ");
//Uses next int as password length
length = scnr.nextInt();
System.out.println("Your password is : "+generatePassword(length));
}


}

  
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// PasswordGenerator.java

package passwordgenerator;

import java.security.SecureRandom;

import java.util.Scanner;

/**

*

* @author Ben Ney

*/

public class PasswordGenerator {

      private static final SecureRandom random = new SecureRandom();

      /** different dictionaries used */

      private static final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

      private static final String ALPHA = "abcdefghijklmnopqrstuvwxyz";

      private static final String NUMERIC = "0123456789";

      private static final String SPECIAL_CHARS = "!@#$%^&*_=+-/";

      public static String generatePassword(int len) {

            Scanner scnr = new Scanner(System.in);

            System.out

                        .println("[1] Only Lowercase Letters\n[2] Only Uppercase Letters\n[3] Only Numbers\n[4] Only Special Characters\n[5] Lowercase & Uppercase\n[6] Lowercase & Numbers\n"

                                    + "[7] Lowercase & Sepcial Characters\n[8] Uppercase & Numbers\n[9] Uppercase & Special Characters\n[10] Numbers & Special Characters\n[11] Lowercase, Uppercase, & Numbers\n"

                                    + "[12] Lowercase, Uppercase, & Special Characters\n[13] Lowercase, Numbers, & Special Characters\n[14] Uppercase, Numbers, Special Characters\n[15] All Characters\n[16] Exit");

            int menuOption = scnr.nextInt();

            while (menuOption != 16) {

                  if (menuOption == 1) {

                        String dic1 = ALPHA;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic1.length());

                              result += dic1.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 2) {

                        String dic2 = ALPHA_CAPS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic2.length());

                              result += dic2.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 3) {

                        String dic3 = NUMERIC;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic3.length());

                              result += dic3.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 4) {

                        String dic4 = SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic4.length());

                              result += dic4.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 5) {

                        String dic5 = ALPHA + ALPHA_CAPS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic5.length());

                              result += dic5.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 6) {

                        String dic6 = ALPHA + NUMERIC;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic6.length());

                              result += dic6.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 7) {

                        String dic7 = ALPHA + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic7.length());

                              result += dic7.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 8) {

                        String dic8 = ALPHA_CAPS + NUMERIC;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic8.length());

                              result += dic8.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 9) {

                        String dic9 = ALPHA_CAPS + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic9.length());

                              result += dic9.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 10) {

                        String dic10 = NUMERIC + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic10.length());

                              result += dic10.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 11) {

                        String dic11 = ALPHA + ALPHA_CAPS + NUMERIC;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic11.length());

                              result += dic11.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 12) {

                        String dic12 = ALPHA + ALPHA_CAPS + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic12.length());

                              result += dic12.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 13) {

                        String dic13 = ALPHA + NUMERIC + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic13.length());

                              result += dic13.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 14) {

                        String dic14 = ALPHA_CAPS + NUMERIC + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic14.length());

                              result += dic14.charAt(index);

                        }

                        return result;

                  } else if (menuOption == 15) {

                        String dic15 = ALPHA + ALPHA_CAPS + NUMERIC + SPECIAL_CHARS;

                        String result = "";

                        for (int i = 0; i < len; i++) {

                              int index = random.nextInt(dic15.length());

                              result += dic15.charAt(index);

                        }

                        return result;

                  } else {

                        System.out.println("Wrong option entered");

                  }

                  System.out

                              .println("[1] Only Lowercase Letters\n[2] Only Uppercase Letters\n[3] Only Numbers\n[4] Only Special Characters\n[5] Lowercase & Uppercase\n[6] Lowercase & Numbers\n"

                                           + "[7] Lowercase & Sepcial Characters\n[8] Uppercase & Numbers\n[9] Uppercase & Special Characters\n[10] Numbers & Special Characters\n[11] Lowercase, Uppercase, & Numbers\n"

                                           + "[12] Lowercase, Uppercase, & Special Characters\n[13] Lowercase, Numbers, & Special Characters\n[14] Uppercase, Numbers, Special Characters\n[15] All Characters\n[16] Exit");

                  menuOption = scnr.nextInt();

            }

            return null;

      }

      // method to check if a password is complex or not

      public static boolean isComplex(String password) {

            // number of unique character types found

            int uniqueTypes = 0;

            // flags to check whichever characters are found

            boolean alphaFound = false, alphaCapsFound = false, numericFound = false, specialFound = false;

            // looping through the password

            for (int i = 0; i < password.length(); i++) {

                  // checking if ALPHA_CAPS contain the current character

                  if (ALPHA_CAPS.contains("" + password.charAt(i))) {

                        // if not already found, making alphaCapsFound true and

                        // incrementing count of unique types

                        if (!alphaCapsFound) {

                              alphaCapsFound = true;

                              uniqueTypes++;

                        }

                  }

                  // doing the same for other types too

                  if (ALPHA.contains("" + password.charAt(i))) {

                        if (!alphaFound) {

                              alphaFound = true;

                              uniqueTypes++;

                        }

                  }

                  if (NUMERIC.contains("" + password.charAt(i))) {

                        if (!numericFound) {

                              numericFound = true;

                              uniqueTypes++;

                        }

                  }

                  if (SPECIAL_CHARS.contains("" + password.charAt(i))) {

                        if (!specialFound) {

                              specialFound = true;

                              uniqueTypes++;

                        }

                  }

                  // at the end of a loop, if uniqueTypes is 3 or greater, then the

                  // password is complex

                  if (uniqueTypes >= 3) {

                        return true;

                  }

            }

            // if the program reach here, that means the password is not complex

            return false;

      }

      public static void main(String[] args) {

            Scanner scnr = new Scanner(System.in);

            String ch = "y", pass;

            int length;

            // looping

            do {

                  // Prompts user to enter length of passoword

                  System.out.print("Enter the length of the password: ");

                  // Uses next int as password length

                  length = scnr.nextInt();

                  // generating password

                  pass = generatePassword(length);

                  // displaying it

                  System.out.println("Your password is : " + pass);

                  // checking if password is complex or not using the isComplex

                  // method, displaying it using ternary operator

                  // ternary operator is in the format condition?statement1:statement2

                  // if condition is true, statement 1 is returned/executed, else

                  // statement2

                  System.out.println("The password is "

                              + (isComplex(pass) ? "complex" : "not complex"));

                 

                  //asking if user wants to continue

                  System.out.print("\nDo you want to try another? (y/n) ");

                  ch = scnr.next();

            } while (ch.equalsIgnoreCase("y"));

            scnr.close();

      }

}

/*OUTPUT*/

Enter the length of the password: 5

[1] Only Lowercase Letters

[2] Only Uppercase Letters

[3] Only Numbers

[4] Only Special Characters

[5] Lowercase & Uppercase

[6] Lowercase & Numbers

[7] Lowercase & Sepcial Characters

[8] Uppercase & Numbers

[9] Uppercase & Special Characters

[10] Numbers & Special Characters

[11] Lowercase, Uppercase, & Numbers

[12] Lowercase, Uppercase, & Special Characters

[13] Lowercase, Numbers, & Special Characters

[14] Uppercase, Numbers, Special Characters

[15] All Characters

[16] Exit

15

Your password is : 19Jfv

The password is complex

Do you want to try another? (y/n) y

Enter the length of the password: 10

[1] Only Lowercase Letters

[2] Only Uppercase Letters

[3] Only Numbers

[4] Only Special Characters

[5] Lowercase & Uppercase

[6] Lowercase & Numbers

[7] Lowercase & Sepcial Characters

[8] Uppercase & Numbers

[9] Uppercase & Special Characters

[10] Numbers & Special Characters

[11] Lowercase, Uppercase, & Numbers

[12] Lowercase, Uppercase, & Special Characters

[13] Lowercase, Numbers, & Special Characters

[14] Uppercase, Numbers, Special Characters

[15] All Characters

[16] Exit

5

Your password is : mpWgtyYXxz

The password is not complex

Do you want to try another? (y/n) y

Enter the length of the password: 20

[1] Only Lowercase Letters

[2] Only Uppercase Letters

[3] Only Numbers

[4] Only Special Characters

[5] Lowercase & Uppercase

[6] Lowercase & Numbers

[7] Lowercase & Sepcial Characters

[8] Uppercase & Numbers

[9] Uppercase & Special Characters

[10] Numbers & Special Characters

[11] Lowercase, Uppercase, & Numbers

[12] Lowercase, Uppercase, & Special Characters

[13] Lowercase, Numbers, & Special Characters

[14] Uppercase, Numbers, Special Characters

[15] All Characters

[16] Exit

14

Your password is : *D0B@LUR!*2G#-4WY*VG

The password is complex

Do you want to try another? (y/n) n

Add a comment
Know the answer?
Add Answer to:
In Java, I have created a program which can generate passwords. Currently the program can generate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just started working on some code for a password creator. I was doing some testing and I keep g...

    I just started working on some code for a password creator. I was doing some testing and I keep getting this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at PasswordCreater.check(PasswordCreater.java:56) at Password.main(Password.java:23 In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special...

  • Create a program via Java that has atleast 8 characters long, one upper case, and one...

    Create a program via Java that has atleast 8 characters long, one upper case, and one lower case, and one digit. here is my code:     public static void main(String[] args)     {         //variables defined         String passwords;         char password =0;                 //settings up password condition to false         boolean passwordcondition= false;         boolean size=false;         boolean digit=false;         boolean upper=false;         boolean lower=false;         //inputting a scanner into the system         Scanner keyboard = new Scanner(System.in);...

  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT