Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /*
- * File Name: RandomStringGenerator.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 main class file for the Random String Generator application. This program
- * will take user input, including type of string (mixed or single), and string length
- * then produce a string of said length and type. The user can then choose to either
- * produce another string, or finish the application. Error checking keeps the user from
- * entering any bad data.
- *
- */
- public class RandomStringGenerator {
- public static void main (String args[]) {
- int inputLength = 0;
- char inputType = 'a';
- char inputRepeat = 'y';
- // These variables let us define what options we allow for certain user input later on.
- char[] errorCheckLength = {'a','b','c','d','e','A','B','C','D','E'};
- char[] errorCheckRepeat = {'y','n','Y','N'};
- boolean keepAlive = true;
- // These variables store the basic text output used in various parts of the program.
- String programHeader = "***************************************\r\n* Random String Generator *\r\n***************************************";
- String textType = "\r\nWhat Type Of String Do You Want?\r\n\r\nA = Mixed Alpha Numeric w/Symbols\r\nB = Mixed Alpha Numeric\r\nC = Just Alpha\r\nD = Just Numeric\r\nE = Just Symbols\r\n";
- String textLength = "\r\nHow long would you likle the string of characters to be?";
- String textRepeat = "\r\n\r\nWould you like to generate another string?\r\nType a 'y' for Yes, or 'n' for No.";
- String textGenerated = "\r\nHere is your generated random string: \r\n";
- String inputText = "\r\nPlease Enter Your Choice Here: ";
- // Here are the two objects we need for this program.
- InputHandler getInput = new InputHandler();
- RandomGenerator getString = new RandomGenerator();
- // This is the fancy little header. I like to make things look pretty.
- System.out.println(programHeader);
- // Now we start our loop that does the actually generation. As long as the user hits 'y' when
- // the program asks, this stays true.
- while (keepAlive==true) {
- // Here we start using our two separate input handlers to get settings from the user.
- System.out.print(textType);
- inputType = getInput.inputChar(inputText,errorCheckLength);
- System.out.print(textLength);
- inputLength = getInput.inputInt(inputText);
- System.out.print(textGenerated);
- // Now we call to the Random generator to get the actual random string they requested.
- ArrayList<String> outputRnd = getString.randGenerator(inputType,inputLength);
- // An enhanced for loop to walk through the array containing the random string.
- for (String a:outputRnd) {
- System.out.print(a);
- }
- // We ask the user if they want to generate another random string.
- System.out.print(textRepeat);
- inputRepeat = getInput.inputChar(inputText,errorCheckRepeat);
- // A simple if else to test what answer the user gave, and either repeat or stop.
- if (inputRepeat == 'y' || inputRepeat == 'Y') {
- keepAlive = true;
- }
- else if (inputRepeat == 'n' || inputRepeat == 'N') {
- keepAlive = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement