I have program that randomly generates a password for a text file of usernames. I was wondering how I could check and make sure the user does not enter a invalid input and throw and error. I don't understand how to do a loop to check for valid inputs.
import java.util.Random;
import java.io.File;
import java.util.Scanner;
import java.io.PrintStream;
import java.lang.Math;
//Create class PasswordProgram
public class PasswordProgram {
public static void main(String[] args) throws Exception {
Random rand = new Random();
// Read in the text file with usernames
File infile = new File(args[0]);
File outfile = new File("PasswordList.txt");
Scanner kbd = new Scanner(System.in);
Scanner Infile = new Scanner(infile);
// Allowing All types of characters to be added
PrintStream Outfile = new PrintStream(outfile);
String uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowers = "abcdefghijklmnopqrstuvwxyz";
String digits = "0123456789";
String symbols = "`~!$%^&*()_+{}|:<>?/.,';][=-";
String total = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789`~!$%^&*()_+{}|:<>?/.,';][=-";
outfile.setReadable(true, true);
// Ask user how many uppercase letters they want in their password
System.out.print("Minumum uppercase: ");
int Uppers = kbd.nextInt();
// Ask user how many lowercase letters they want in their password
System.out.print("Minumum lowercase: ");
int Lowers = kbd.nextInt();
// Ask user how many digits they want in their password
System.out.print("Minumum digits: ");
int Digits = kbd.nextInt();
// Ask user how many symbols they want in their program
System.out.print("Minumum symbols: ");
int Symbols = kbd.nextInt();
// Ask user how many total characters they want their password
System.out.print("Minumum total characters: ");
int Total = kbd.nextInt();
char[] password = new char[Total];
String user;
int i, j, k;
char temp;
// As long as there is more usernames add more passwords
while (Infile.hasNext()) {
user = Infile.next();
// Create random uppercase letters
for (i = 0; i < Uppers; i++) {
password[i] = uppers.charAt(Math.abs(rand.nextInt()) % 26);
}
// Create random lowercase letters and add them to uppercase
for (i = Uppers; i < (Uppers + Lowers); i++) {
password[i] = lowers.charAt(Math.abs(rand.nextInt()) % 26);
}
// Create random digits and add them to previous uppercase and
// lowercase
for (i = Uppers + Lowers; i < (Uppers + Lowers + Digits); i++) {
password[i] = digits.charAt(Math.abs(rand.nextInt()) % 10);
}
// Create random symbols and add them to previous uppercase,
// lowercase, and digits
for (i = Uppers + Lowers + Digits; i < (Uppers + Lowers + Digits + Symbols); i++) {
password[i] = symbols.charAt(Math.abs(rand.nextInt()) % 26);
}
// Create total characters and make sure the password as at least
// that length with uppercase, lowercase, digits, and symbols
for (i = Uppers + Lowers + Digits + Symbols; i < Total; i++) {
password[i] = total.charAt(Math.abs(rand.nextInt()) % 88);
}
// Print the generated passwords for each username
System.out.println(user + " " + (new String(password)));
Outfile.println(user + " " + (new String(password)));
}
}
}