Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class PasswordGenerator
  5. {
  6.     public static void main(String args[]) throws IOException
  7.     {      
  8.         // initialize the input and output objects.
  9.         Scanner input = new Scanner(new File ("userNames.txt"));
  10.         PrintWriter output = new PrintWriter(new File("passwords.txt"));
  11.        
  12.         final int LENGTH = 8;
  13.        
  14.         output.println("User" + "    " + "Password");
  15.        
  16.         String password = "";
  17.        
  18.         while (input.hasNext()) {
  19.             String userName = input.nextLine();
  20.        
  21.             for (int i = 1 ; i < LENGTH; i++){
  22.                 int charChoice = 1 + (int) (Math.random() * 4);
  23.                 char curl = ' ';
  24.            
  25.                 switch(charChoice){
  26.                     case 1: curl = getLowerCaseCharacter(); break;
  27.                     case 2: curl = getUpperCaseCharacter(); break;
  28.                     case 3: curl = getSpecialCharacter(); break;
  29.                     case 4: curl = getRandomDigitCharacter(); break;
  30.                 }// end the switch statement
  31.                
  32.                 password += curl;
  33.             }// end the for loop
  34.             System.out.println(userName + "    " + password);
  35.             password = "";
  36.         }// end the while loop
  37.     } // End method main
  38.    
  39.     public static char getRandomCharacter(char ch1, char ch2) {
  40.         return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
  41.     }
  42.    
  43.     public static char getLowerCaseCharacter(){
  44.         return getRandomCharacter('a', 'z');
  45.     }
  46.    
  47.     public static char getUpperCaseCharacter(){
  48.         return getRandomCharacter('A', 'Z');
  49.     }
  50.    
  51.     public static char getSpecialCharacter(){
  52.         char special = ' ';
  53.         int specialNum = 1 + (int) Math.random() * 7;
  54.         switch(specialNum){
  55.             case 1: special = '!'; break;
  56.             case 2: special = '$'; break;
  57.             case 3: special = '%'; break;
  58.             case 4: special = '&'; break;
  59.             case 5: special = '('; break;
  60.             case 6: special = ')'; break;
  61.             case 7: special = '?'; break;
  62.         }// end the swith
  63.         return special;
  64.     }// end returnSpecialCharacter Method
  65.    
  66.     public static char getRandomDigitCharacter(){
  67.         return getRandomCharacter('0' , '9');
  68.     }
  69. } /// End class <program_name>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement