fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.security.SecureRandom;
  5. /**
  6.   * @author re
  7.   * http://j...content-available-to-author-only...i.al
  8.   */
  9. class Ideone
  10. {
  11. String charactersToBeUsed;
  12.  
  13. private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  14. private static final int DEFAULT_PASSWORD_LENGTH = 8;
  15.  
  16. // default constructor
  17. Ideone() { this(UPPERCASE_CHARACTERS); }
  18. // constructor
  19. Ideone(String characters) { this.charactersToBeUsed = characters; }
  20.  
  21. public String generate() { return generate(DEFAULT_PASSWORD_LENGTH); }
  22.  
  23. public String generate(int length) {
  24. char[] password = new char[length];
  25.  
  26. char[] possibleCharacters = charactersToBeUsed.toCharArray();
  27.  
  28. for (int i = 0; i < length; i++) {
  29. password[i] = possibleCharacters[r.nextInt(possibleCharacters.length)];
  30. }
  31. return new String(password);
  32. }
  33.  
  34. private enum EnumAsciiCharacters {
  35. UPPERCASE( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ),
  36. LOWERCASE( "abcdefghijklmnopqrstuvwxyz" ),
  37. NUMERIC( "1234567890" ),
  38. SPECIAL( "~!@#$%^&*()_|" );
  39.  
  40. private String characters;
  41.  
  42. @Override
  43. public String toString() {
  44. return this.characters;
  45. }
  46.  
  47. private EnumAsciiCharacters( String characters ) {
  48. this.characters = characters;
  49. }
  50. } // EnumAsciiCharacters
  51.  
  52. public static void main (String[] args) throws java.lang.Exception
  53. {
  54. String useThisCharacters =
  55. String.format(
  56. "%s%s%s",
  57. EnumAsciiCharacters.UPPERCASE, EnumAsciiCharacters.LOWERCASE, EnumAsciiCharacters.SPECIAL
  58. );
  59. Ideone pg = new Ideone(useThisCharacters);
  60. System.out.println(pg.generate());
  61. }
  62. }
Success #stdin #stdout 0.13s 320448KB
stdin
Standard input is empty
stdout
vbtRZmo@