Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /*
- * File Name: RandomGenerator.java
- * Package Name: N/A
- * Application Name: Random String Generator
- *
- * Version Info: 1.0.0
- * Version Date: 09-09-2014
- *
- * Creator: Corey D. Eacret
- * Website: http://www.appdappdev.com
- * Contact: appdapp@gmail.com
- *
- * Description:
- * This is the class for the Random String Generator that actually creates the random
- * strings. It contains 3 nested classes that produce a random single digit integer,
- * random upper or lower case alpha character, or one of a list of approved symbols.
- * The class will output an ArrayList with each index containing one of the randomly
- * generated characters.
- *
- */
- public class RandomGenerator {
- // We start by declaring the length and type variables, which we get from user input
- // later on in the program.
- char randType = 'a';
- int randLength = 0;
- // This is our first nested class, the one that produces a random number.
- class numGen {
- int rNum() {
- // The actual random function, gets a number between 0 and 9.
- int rN = (int) (Math.random() * 10);
- return rN;
- }
- }
- // This is our second nested class, this one produces a random alpha character.
- class alphaGen {
- char rAlpha() {
- // We start by getting a random number, between 0 and 51.
- int rN = (int) (Math.random() * 52);
- // We then use that number to pick a random starting point, 'A' or 'a'.
- char base = (rN < 26) ? 'A':'a';
- // Finally, we use our starting point, and random number, to pick our alpha char.
- char rA = (char) (base + rN % 26);
- return rA;
- }
- }
- // Our final nested class, this one uses a list of symbols, and chooses a random one.
- class symGen {
- char rSymbol() {
- // The symbol list, this allows us to only use a select few symbols.
- char[] symList = {'!','@','#','$','%','^','&','*','(',')','-','_','=','+',',','.','<','>','/','?',';',':','`','~'};
- // We get our random value, a number between 0, and the amount of symbols.
- int rN = (int) (Math.random() * 24);
- // Finally, we use our random number to pick one of the symbols from the list.
- char rS = symList[rN];
- return rS;
- }
- }
- // Now we create a new object for each of our random generators.
- RandomGenerator.numGen getRandNum = new RandomGenerator.numGen();
- RandomGenerator.alphaGen getRandAlpha = new RandomGenerator.alphaGen();
- RandomGenerator.symGen getRandSym = new RandomGenerator.symGen();
- // This is the method that takes the user input, and then creates the random string.
- public ArrayList<String> randGenerator(int randType, int randLength) {
- // Our ArrayList which is the output at the end of this class.
- ArrayList<String> outputString = new ArrayList<String>();
- // Start of our if else list, that uses the user input to select the type of string.
- if (randType == 'a' || randType == 'A') {
- // For statement to generate a random character for the length requested.
- for (int aType=0;aType!=randLength;aType++) {
- // We use the random number generator to also create a random number that
- // we use to decide which type (num,alpha,sym) for each spot.
- String rndOutput;
- int whichType = getRandNum.rNum();
- if (whichType < 4) {
- // Here we get a random alpha character, and convert char to string.
- rndOutput = Character.toString(getRandAlpha.rAlpha());
- outputString.add(rndOutput);
- }
- else if (whichType > 3 && whichType < 7) {
- // Here we get a random number, and convert int to string.
- rndOutput = Integer.toString(getRandNum.rNum());
- outputString.add(rndOutput);
- }
- else if (whichType > 6) {
- // Here we get a random symbol, and conert char to string.
- rndOutput = Character.toString(getRandSym.rSymbol());
- outputString.add(rndOutput);
- }
- }
- }
- // Here we get our string, if type is set to 'b'. The functions inside are the same
- // as above, so there won't be comments on the rest of these.
- else if (randType == 'b' || randType == 'B') {
- for (int aType=0;aType!=randLength;aType++) {
- String rndOutput;
- int whichType = getRandNum.rNum();
- if (whichType < 5) {
- rndOutput = Character.toString(getRandAlpha.rAlpha());
- outputString.add(rndOutput);
- }
- else if (whichType > 4) {
- rndOutput = Integer.toString(getRandNum.rNum());
- outputString.add(rndOutput);
- }
- }
- }
- // Here we get our string for 'c' type.
- else if (randType == 'c' || randType == 'C') {
- for (int aType=0;aType!=randLength;aType++) {
- String rndOutput;
- rndOutput = Character.toString(getRandAlpha.rAlpha());
- outputString.add(rndOutput);
- }
- }
- // Here we get our string for 'd' type.
- else if (randType == 'd' || randType == 'D') {
- for (int aType=0;aType!=randLength;aType++) {
- String rndOutput;
- rndOutput = Integer.toString(getRandNum.rNum());
- outputString.add(rndOutput);
- }
- }
- // Here we get our string for 'e' type.
- else if (randType == 'e' || randType == 'E' ) {
- for (int aType=0;aType!=randLength;aType++) {
- String rndOutput;
- rndOutput = Character.toString(getRandSym.rSymbol());
- outputString.add(rndOutput);
- }
- }
- // Even with error checking, we leave this in just in case funny things happen.
- else {
- System.out.println("INVALID TYPE!!");
- }
- // Here we send out our completed random string!!
- return outputString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement