Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class PasswordGenerator
- {
- public static void main(String args[]) throws IOException
- {
- // initialize the input and output objects.
- Scanner input = new Scanner(new File ("userNames.txt"));
- PrintWriter output = new PrintWriter(new File("passwords.txt"));
- final int LENGTH = 8;
- output.println("User" + " " + "Password");
- String password = "";
- while (input.hasNext()) {
- String userName = input.nextLine();
- for (int i = 1 ; i < LENGTH; i++){
- int charChoice = 1 + (int) (Math.random() * 4);
- char curl = ' ';
- switch(charChoice){
- case 1: curl = getLowerCaseCharacter(); break;
- case 2: curl = getUpperCaseCharacter(); break;
- case 3: curl = getSpecialCharacter(); break;
- case 4: curl = getRandomDigitCharacter(); break;
- }// end the switch statement
- password += curl;
- }// end the for loop
- System.out.println(userName + " " + password);
- password = "";
- }// end the while loop
- } // End method main
- public static char getRandomCharacter(char ch1, char ch2) {
- return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
- }
- public static char getLowerCaseCharacter(){
- return getRandomCharacter('a', 'z');
- }
- public static char getUpperCaseCharacter(){
- return getRandomCharacter('A', 'Z');
- }
- public static char getSpecialCharacter(){
- char special = ' ';
- int specialNum = 1 + (int) Math.random() * 7;
- switch(specialNum){
- case 1: special = '!'; break;
- case 2: special = '$'; break;
- case 3: special = '%'; break;
- case 4: special = '&'; break;
- case 5: special = '('; break;
- case 6: special = ')'; break;
- case 7: special = '?'; break;
- }// end the swith
- return special;
- }// end returnSpecialCharacter Method
- public static char getRandomDigitCharacter(){
- return getRandomCharacter('0' , '9');
- }
- } /// End class <program_name>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement