import java.util.Scanner;
import java.util.Random;
public class SecretPasscodes {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
System.out.println("#########################|Password Generator|#######################");
System.out.println("# [1] Lowercase Letters #");
System.out.println("# [2] Uppercase and Lowercase Letters #");
System.out.println("# [3] Letters and numbers #");
System.out.println("# [4] Lowercase and Uppercase letters, Numbers , and symbols #");
System.out.println("# [5] Quit #");
System.out.println("####################################################################");
System.out.println("Enter your selection(1-5)");
// Variables
int choice = in.nextInt();
System.out.println("Password Length(1-14");
int passLength = in.nextInt();
// Lowercase
if (choice == 1) {
for (int counter = 0; counter < passLength; counter++) {
int lowerLetter = rand.nextInt((122 - 97) + 1) + 97;
System.out.print((char) lowerLetter);
}
// Uppercase + lowercase
} else if (choice == 2) {
for (int counter = 0; counter < passLength; counter++) {
int ascii = rand.nextInt(255);
while ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
System.out.print((char) ascii);
}
}
}
}
}
The for loop will not be infinite if the while loop is removed. Once I add the while loop in , the for loop becomes infinite. I have been stuck on this for a while and my apcs teacher is not responding.
Thanks!
int ascii = rand.nextInt(255);needs to be inside the loop, and you need toifit's in the acceptable range, then print and break if it is. Better yet, why not try and construct a printable value instead? Use two rands, one to pick a letter and one to pick a case, or nextInt(52) and split that into upper and lower case, or something else?