I wrote this program recently to generate random passwords for myself. It works well but it's been a long time since I wrote any java and am just looking on ways to clean up the code. I'm also not sure how truly "random" the password generation is or if it is too excessive.
import java.util.Random;
import java.util.Scanner;
final class RNG
{
final public static void main(String[] args)
{
System.out.print("This program generates between 1 and 100 RANDOM and SECURE passwords.\nPasswords are not stored in any way.\nWRITE DOWN YOUR NEW PASSWORDS!\nPassword length must be between 12 and 120.\n\n");
System.out.print("Please enter the length for your new password(s): ");
Scanner input = new Scanner(System.in);
if(input.hasNextLine())
{
System.out.print("Please enter the number of passwords you wish to create: ");
Scanner number = new Scanner(System.in);
RNG myPW = new RNG();
myPW.testLength(input, number);
}
}
final private void testLength(Scanner test, Scanner amount)
{
if(test.hasNextInt() && amount.hasNextInt())
{
int PWlength = test.nextInt();
int numberOfPW = amount.nextInt();
if ((PWlength >= 12 && PWlength <= 120) && numberOfPW > 0 && numberOfPW <= 100)
{
RNG myPW = new RNG();
myPW.PWGenerator(PWlength, numberOfPW);
}
if ((PWlength < 12 || PWlength > 120) && numberOfPW > 0 && numberOfPW <= 100)
{
System.out.println("ERROR! LENGTH OF PASSWORD MUST BE BETWEEN 12 AND 120\n");
restart();
}
if ((PWlength >= 12 && PWlength <= 120) && numberOfPW <= 0)
{
System.out.println("ERROR! NUMBER OF PASSWORDS YOU WISH TO GENERATE MUST BE GREATER THAN ZERO\n");
restart();
}
if ((PWlength >= 12 && PWlength <= 120) && numberOfPW > 100)
{
System.out.println("ERROR! NUMBER OF PASSWORDS YOU WISH TO GENERATE MUST BE LESS THAN OR EQUAL TO 100\n");
restart();
}
if((PWlength < 12 || PWlength > 120) && numberOfPW <= 0)
{
System.out.println("ERROR! LENGTH OF PASSWORD MUST BE BETWEEN 10 AND 120");
System.out.println("ERROR! NUMBER OF PASSWORDS YOU WISH TO GENERATE MUST BE GREATER THAN ZERO\n");
restart();
}
if((PWlength < 12 || PWlength > 120) && numberOfPW > 100)
{
System.out.println("ERROR! LENGTH OF PASSWORD MUST BE BETWEEN 10 AND 120");
System.out.println("ERROR! NUMBER OF PASSWORDS YOU WISH TO GENERATE MUST BE LESS THAN OR EQUAL TO 100\n");
restart();
}
}
if(!test.hasNextInt() && amount.hasNextInt())
{
System.out.println("ERROR! LENGTH OF PASSWORD IS NOT AN INTEGER. PLEASE TRY AGAIN.\n");
restart();
}
if(test.hasNextInt() && !amount.hasNextInt())
{
System.out.println("ERROR! NUMBER OF PASSWORDS IS NOT AN INTEGER. PLEASE TRY AGAIN.\n");
restart();
}
else
{
System.out.println("ERROR! LENGTH OF PASSWORD IS NOT AN INTEGER. PLEASE TRY AGAIN.");
System.out.println("ERROR! NUMBER OF PASSWORDS IS NOT AN INTEGER. PLEASE TRY AGAIN.\n");
restart();
}
}
final private void restart()
{
System.out.print("Please enter the length for your new password(s): ");
Scanner input = new Scanner(System.in);
if(input.hasNextLine())
{
System.out.print("Please enter the number of passwords you wish to create: ");
Scanner number = new Scanner(System.in);
RNG myPW = new RNG();
myPW.testLength(input, number);
}
}
final private void PWGenerator(int a, int b)
{
Random randomInt = new Random();
Random randomLetterU = new Random();
Random randomLetterL = new Random();
Random randomChar = new Random();
String[] alphabetUPPER = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
String[] alphabetlower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String[] characters = {"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "?", "/", "<", ">"};
int selection;
int c;
int e = 1;
System.out.println();
while(e <= b)
{
System.out.print("Password #" + e + ": ");
int d = 1;
while(d <= a)
{
c = randomInt.nextInt(4);
if (c == 0)
{
selection = randomLetterU.nextInt(26);
System.out.print(alphabetUPPER[selection]);
d++;
}
if (c == 1)
{
selection = randomLetterL.nextInt(26);
System.out.print(alphabetlower[selection]);
d++;
}
if (c == 2)
{
System.out.print(randomInt.nextInt(10));
d++;
}
if (c == 3)
{
selection = randomChar.nextInt(18);
System.out.print(characters[selection]);
d++;
}
}
System.out.println("\n");
e++;
}
if(e > b)
{System.exit(0);}
}
}