Guest User

[Java] Random Password Generator

a guest
Aug 20th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. /* You're challenge for today is to create a random password generator!
  2. For extra credit, allow the user to specify the amount of passwords to generate.
  3. For even more extra credit, allow the user to specify the length of the strings he wants to generate!
  4. */
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class easy4{
  9.     public static void main(String args[]){
  10.         int userInput = 0;
  11.         int counter = 0;
  12.         String password;
  13.        
  14.         System.out.println("Welcome to the Random password Generator! \nHow many passwords would you like to generate?");
  15.         counter = input();
  16.        
  17.         System.out.println("How many characters would you like the passwords to be?");
  18.         userInput = input();
  19.  
  20.         for(int i = 1; i<=counter; i++){
  21.             password = passwordGenerator(userInput);
  22.             System.out.println("Password " + i + ": " + password);
  23.         }
  24.     }
  25.     public static int input(){     
  26.         Scanner inputs = new Scanner(System.in);
  27.         int userInput = inputs.nextInt();
  28.         return userInput;
  29.     }
  30.     public static String passwordGenerator(int userInput){                     
  31.         StringBuilder passwords = new StringBuilder(userInput);                
  32.         String list = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_=+{}[]<>,.?/");   //List of usable characters
  33.         Random num = new Random();
  34.                                        
  35.         for(int i = 0; i<userInput; i++){                              
  36.             passwords.append(list.charAt(num.nextInt(list.length()))); 
  37.                 }  
  38.         String password = passwords.toString();
  39.         return password;
  40.     }
  41. }
Add Comment
Please, Sign In to add comment