Advertisement
Guest User

RandomStringGenerator

a guest
Sep 11th, 2014
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.util.ArrayList;
  2. /*
  3.  * File Name: RandomStringGenerator.java
  4.  * Package Name: N/A
  5.  * Application Name: Random String Generator
  6.  *
  7.  * Version Info: 1.0.0
  8.  * Version Date: 09-09-2014
  9.  *
  10.  * Creator: Corey D. Eacret
  11.  * Website: http://www.appdappdev.com
  12.  * Contact: appdapp@gmail.com
  13.  *
  14.  * Description:
  15.  * This is the main class file for the Random String Generator application. This program
  16.  * will take user input, including type of string (mixed or single), and string length
  17.  * then produce a string of said length and type. The user can then choose to either
  18.  * produce another string, or finish the application. Error checking keeps the user from
  19.  * entering any bad data.
  20.  *
  21.  */
  22.  
  23.  
  24. public class RandomStringGenerator {
  25.    
  26.     public static void main (String args[]) {
  27.        
  28.         int inputLength = 0;
  29.         char inputType = 'a';
  30.         char inputRepeat = 'y';
  31.         // These variables let us define what options we allow for certain user input later on.
  32.         char[] errorCheckLength = {'a','b','c','d','e','A','B','C','D','E'};
  33.         char[] errorCheckRepeat = {'y','n','Y','N'};
  34.         boolean keepAlive = true;
  35.        
  36.         // These variables store the basic text output used in various parts of the program.
  37.         String programHeader = "***************************************\r\n*       Random String Generator       *\r\n***************************************";
  38.         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";
  39.         String textLength = "\r\nHow long would you likle the string of characters to be?";
  40.         String textRepeat = "\r\n\r\nWould you like to generate another string?\r\nType a 'y' for Yes, or 'n' for No.";
  41.         String textGenerated = "\r\nHere is your generated random string: \r\n";
  42.         String inputText = "\r\nPlease Enter Your Choice Here:  ";
  43.        
  44.         // Here are the two objects we need for this program.
  45.         InputHandler getInput = new InputHandler();
  46.         RandomGenerator getString = new RandomGenerator();
  47.        
  48.         // This is the fancy little header. I like to make things look pretty.
  49.         System.out.println(programHeader);
  50.        
  51.         // Now we start our loop that does the actually generation. As long as the user hits 'y' when
  52.         // the program asks, this stays true.
  53.         while (keepAlive==true) {
  54.            
  55.             // Here we start using our two separate input handlers to get settings from the user.
  56.             System.out.print(textType);
  57.             inputType = getInput.inputChar(inputText,errorCheckLength);
  58.             System.out.print(textLength);
  59.             inputLength = getInput.inputInt(inputText);
  60.             System.out.print(textGenerated);
  61.            
  62.             // Now we call to the Random generator to get the actual random string they requested.
  63.             ArrayList<String> outputRnd = getString.randGenerator(inputType,inputLength);
  64.            
  65.             // An enhanced for loop to walk through the array containing the random string.
  66.             for (String a:outputRnd) {
  67.                 System.out.print(a);
  68.             }
  69.            
  70.             // We ask the user if they want to generate another random string.
  71.             System.out.print(textRepeat);
  72.             inputRepeat = getInput.inputChar(inputText,errorCheckRepeat);
  73.            
  74.             // A simple if else to test what answer the user gave, and either repeat or stop.
  75.             if (inputRepeat == 'y'  || inputRepeat == 'Y') {
  76.                 keepAlive = true;
  77.             }
  78.             else if (inputRepeat == 'n' || inputRepeat == 'N') {
  79.                 keepAlive = false;
  80.             }
  81.            
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement