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!
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.
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.
