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

4
  • 3
    I don't think you want a while loop there. I guess you're trying to repeat the rand.nextInt until it gives you a value in range? In that case the int ascii = rand.nextInt(255); needs to be inside the loop, and you need to if it'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? Commented Dec 11, 2017 at 0:28
  • 1
    Another (maybe against the spirit) way to do things like this is to define a string for each set, e.g. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" and then randomly choose a character from that string. Then you don't need to worry about ASCII codes at all. Commented Dec 11, 2017 at 0:29
  • You are genenrating random integers and it is possible that you get always numbers between those ranges, Commented Dec 11, 2017 at 0:34
  • technically the for loop does not become infinite. It never exceeds one iteration if you get a bad integer the first time. Commented Dec 11, 2017 at 0:41

2 Answers 2

2

The reason this is failing for you is you assign ascii just prior to entering the while loop. This value is not changed in the while loop, so it will always contain the value you set it to prior to entering the while loop. You either need to change your while conditions, or you need to modify ascii inside of your while block such that it no longer meets the conditions specified. You could also add break after your writeline. (Making it work just like an if statement). Your code could be summarized as:

While(1)
{
    print(1)
}
Sign up to request clarification or add additional context in comments.

Comments

1

I presume you want to do something like this:

else if (choice == 2) {
    for (int counter = 0; counter < passLength; counter++) {
        int ascii = rand.nextInt(255);
        while (true) {
            if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
                System.out.print((char) ascii);
                break;
            } else {
                ascii = rand.nextInt(255);
            }
        }
    }
     System.out.println();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.