Skip to content
WJunction - Webmaster Forum

Tutorials & Guides [TUT] Java passgenerator encapsulated

Status
Not open for further replies.

Dom

Experienced Member
608
2010
53
This is my encapsulated version of a passwordgenerator in java. You create a new class in your project (i recommend you to use the name Passgenerate to avoid errors). The later you can call the function by typing:

Code:
Passgenerate.Generate(properties)


Code:
package YoupackageName; // change YoupackageName to you package in the project...

import java.util.ArrayList;
import java.util.Random;

public class Passgenerate {

    public static String Generate(int lenght, boolean Upperlower, boolean alphanumeric) {
        String[] lowercase = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        String[] Uppercase = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        String[] Numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String txtOutput="";

        ArrayList<String> objlist = new ArrayList<String>();
        Random rnd = new Random();

        int i;

        for (i = 0; i < lowercase.length; i++) {
            objlist.add(lowercase[i]);
        }
        if (Upperlower == true) {
            for (i = 0; i < Uppercase.length; i++) {
                objlist.add(Uppercase[i]);
            }
        }
        if (alphanumeric == true) {
            for (i = 0; i < Numbers.length; i++) {
                objlist.add(Numbers[i]);
            }
        }
        for (i = 0; i < lenght; i++) {
            txtOutput = txtOutput + objlist.get(rnd.nextInt(objlist.size()));
        }
        return txtOutput;
    }
}
If you encounter some projects when using this class please post it so i can edit it
 
Status
Not open for further replies.

About the author

D
Experienced Member · Joined
608
Messages
53
Reactions
28
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom