How good is my password generator code? (Java)

Paper Cars

Audio Production Services
Supreme
Feedback score
8
Posts
514
Reactions
103
Resources
0
I was tasked with writing a simple program that generates an 8 character password.
The password must contain AT LEAST two UPPERCASE letters and AT LEAST 2 numbers.

Here is what I came up with!

Code:
import java.util.*;

class Main {

  public static void generatePassword(){
    String a = Integer.toString(randNum(0,9));
    String b = Integer.toString(randNum(0,9));
    String c = randChar();
    String d = randChar();
    String e = letterNumberDecider();
    String f = letterNumberDecider();
    String g = letterNumberDecider();
    String h = letterNumberDecider();

    System.out.println(buildPassword(a,b,c,d,e,f,g,h));
    
  }
 
  public static void main(String[] args) {

    generatePassword();
    
  }
 
  public static String buildPassword(String a, String b, String c, String d, String e, String f, String g, String h){

    String password = a+b+c+d+e+f+g+h;
    password = shuffleString(password);
    return password;
    
  }
  //String Shuffler
  public static String shuffleString(String string) {
    
    List<String> letters = Arrays.asList(string.split(""));
    Collections.shuffle(letters);
    String shuffled = "";
    
    for (String letter : letters) {
     shuffled += letter;
    }
    
    return shuffled;
    
  }

  //Random Number Generator
  public static int randNum(int min, int max){
    
    int randInt = (int)Math.floor(Math.random()*(max-min+1)+min);
    return randInt;
    
  }

  //random letter generator
  public static String randChar(){

     int capChance = randNum(0,1);
     boolean isCap = false;
      
     Random r = new Random();
     char a = (char) (r.nextInt(26) + 'a');
     String c = Character.toString(a);
    
     if(capChance == 1){
       isCap = true;
     }else{
       isCap = false;
     }

    String randChar;

     if(isCap == true){
       randChar = c.toUpperCase();
     } else {
       randChar = c;
     }
    
     return randChar;
    
  }

  public static String letterNumberDecider(){

    String output = "";
    
    for(int x = 0; x < 3; x++){
      int y = randNum(0,1);
      if(y == 0){
        output = Integer.toString(randNum(0,9));
      }else{
        output = randChar();
      }
    }

    return output;
    
  }

}

Im looking for feedback based on how efficient, and minimal my code is.
For example is there an easier way to do this, does my code feature redundancy, etc.
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

gustavomay

Feedback score
0
Posts
1
Reactions
0
Resources
0
It's been a while since your post, but I wanted to offer some insight. Your code looks pretty solid for generating a password with specific requirements. It’s clear and functional. However, there are a few suggestions to make it more efficient: Combine Functions: You can simplify randChar() and letterNumberDecider() into a single function to reduce redundancy. Random Object: Instead of creating a new Random object in randChar(), create one instance and reuse it. StringBuilder: Use StringBuilder instead of string concatenation in shuffleString() for better performance. Overall, your approach is great, and it's almost as good as this password generator free I use for my needs.
 
Last edited:
Top