.. Write a program in JAVA named PasswordGen that will generate passwords for a
ID: 3557112 • Letter: #
Question
..
Write a program in JAVA named PasswordGen that will generate passwords for a user. The program will ask the user how many passwords they want. It will then read a file named200nouns.txt (will be provided by the auto test)that contains a list of words separated by either spaces or new lines into an array.
Note: the file 200nouns.txt may have between 1 and up to 200 words. You program needs to handle this by only picking from the list of words read in. The computer will then print one password per line onto the console. It will create each password composed of 4 parts: First a random wordfrom the list of words from file, then a random selection from the following special chars:
# - + @ _ % & * /
Then another random word selected from the word array, then finally a random number from 1 to 9.
Here is the program in pseudo code:
- Open the file 200nouns.txt
- Read file into an array that that will hold 200 words and count how many actual words.
- Create an array that has these special symbols: (use an initializer list)
# - + @ _ % & * /
- Ask the user "how many passwords do you want?"
- Input his answer
- Loop for as many times as the user indicated
- Pick a random word out of the word array from just the words that were read in
- Pick another random special symbol from the array of special symbols
- Pick another random word out of the word array from just the words that were read in
- Pick a random number from 1 to 9
- Compose the password from the four parts picked above (do not put any blanks between the four components)
They should be in the order:
random word1, random symbol, random word2, and then random number 1 to 9
- End of loop
Example program run: (the 4 is typed by the user)
How many passwords do you want?
4
month-person1
column#idea5
crop&space9
changesong2
You do only need upload the java file, the 200nouns.txt file will be provided by the automated test.
Explanation / Answer
import java.util.*;
import java.io.*;
public class PasswordGen{
public static void main(String args[]){
Random r = new Random();
Scanner in = new Scanner(System.in);
int num, count = 0, i = 0, n;
char symbol[] = {'#', '-', '+', '@', '_', '%', '&', '*', '/', '\'};
String names[] = new String[200];
String password = "";
Scanner x = null;
try{
x = new Scanner(new File("C:/Users/Hacker/Desktop/named200nouns.txt"));
}catch(Exception e){
System.out.println("File not found!");
}
while(x.hasNext()){
names[count++] = x.next();
}
System.out.println("How many passwords do you want?");
n = in.nextInt();
for(int j = 1; j <= n; j++){
num = r.nextInt(count);
password += names[num];
num = r.nextInt(10);
password += symbol[num];
num = r.nextInt(count);
password += names[num];
num = 1 + r.nextInt(9);
password += Integer.toString(num);
System.out.println(password);
password = "";
}
x.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.