I am doing a java assignment and I am trying to generate random passwords of three words (concatenated together) coming from a story (Alice in Wonderland) formed as a multi-dimensional array (https://imgur.com/kczPu4l).
Instructions:
- The page number, paragraph number, and line number are chosen randomly. Use the java.util.Random class to generate random numbers using the nextInt() method.
- Use the split(" ") to convert the String from the chosen line into an array of words
- Choose a random word from the array in Step 2. (Hint: use the length attribute)
- If a word is composed of only a single character (e.g., "a"), or the word contains the newline character ('\n'), go back to Step 1.
This is what I did so far:
import java.util.Random;
public class Assign3Q1 {
public static void main(String[] args) {`
final String[][][] book = { ......} // check the link above
Random r=new Random();
int pageNum = r.nextInt(book.length);
int paraNum = r.nextInt(book[pageNum].length);
int lineNum = r.nextInt(book[pageNum][paraNum].length);
for (int i =0; i<5; i++) { // I did 5 passwords just to be simple. I actually need 10000.
System.out.print("Password = ");
for (int j = 0; j<3; j++) {
String sentence = book[pageNum][paraNum][lineNum];
String[] string = sentence.split(" "); // step 3
int index = new Random().nextInt(string.length);
String randomWord = string[index];
if (randomWord.equals("a") && randomWord.contains("\n")) {
}
else
System.out.print(randomWord);
}
System.out.println("");
}
However, I am having trouble making the conditions for the 3 concatenated words. For example in step 5 it says
- When you have three words, concatenate them to form a password, and apply the following steps. You can use the compareTo() method from the String class (but no HashSet). Do not use any method from the Character class.
a. No two words can be exactly the same (e.g., “theAlicethe” isn’t allowed, but “the” and “The” are accepted). Otherwise, go back to Step 1.
b) The password must have uppercase char. if not go back to step 1.
c) The password must have lowercase char. If not go back to step 1
...... more conditions.
Question 1: Since the passwords are random everytime a loop is done, how do you put multiple conditions inside the for loop I created? Can I do it the same way as I did for step 4 or is there another way?
Question 2: Another problem is the output of the passwords. For some reason whenever I run the program (The program above) one of the passwords starts on a new line. Why is that? For example the first password is supposed to be gavethis"I, but the two words was put in the next line. Same for the thrid password
Password = gave
this"I
Password = sharply."I"I
Password = advisegave
leave
Password = generallyadviseyou
Password = adviseminute!"sharply.
Question 3: Based on the conditions from step 5, when all the conditions for each password are met, how do you create password generation attempts that "failed" using the conditions from step 5?
For example: let's say one of the passwords that met all the conditions was "Shekey;Just," (note: key; and Just, with She are three words). Let's say it took 11 times for the random generator to get to this password because the other passwords before this one had no uppercase when it must contain at least one.
When you run the program the output should look something like this along with the other conditions (NewLine and Single are from step 4). (note: I am assuming the passwords are randomly generated, therefore it may or not be the same number of failed attempts. It may actually take 7 times to find a password with uppercase, or 10 times to find a password with no \n newline etc..)
password = Shekey;Just, NewLine = 17 Single = 2 Equal= 4 Upper = 11 Lower = 0
...
...
Aucun commentaire:
Enregistrer un commentaire