Advertisement
ew0345

StringGenV3_2

May 20th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. //I was bored again so I've made some changes to a previous paste I made, enough that I decided to just make it a new one :p
  2.  
  3. import java.util.Scanner;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.BufferedWriter;
  7. import java.io.FileWriter;
  8.  
  9. public class StringGenV3_2 {
  10.     public static void main(String[] args) {
  11.         String[] rand_c = {"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","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","0","1","2","3","4","5","6","7","8","9","`","~","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","{","]","}",";",":","\'","\"",",","<",".",">","/","?","\\","|"};
  12.         File file = new File(System.getProperty("user.home")+"/Desktop/RandomStrings.txt");
  13.  
  14.         try (Scanner in = new Scanner(System.in)) {
  15.             System.out.print("\nWrite Strings to file: ");
  16.             String answer = in.nextLine();
  17.  
  18.             System.out.print("Amount of Strings to Generate: ");
  19.             int amount = in.nextInt();
  20.  
  21.             System.out.print("String Length: ");
  22.             int slength = in.nextInt();
  23.  
  24.             System.out.print("Letters Only (1), Letters and Numbers (2), All (3): ");
  25.             int type = in.nextInt();
  26.            
  27.             System.out.print("\n");
  28.  
  29.             switch (answer.toLowerCase()) {
  30.                 case "yes":case "y":case "true":case "t":case "1":
  31.                     if (type != 1 && type != 2 && type != 3) {
  32.                         System.out.println("Defaulted to mode: All");
  33.                     }
  34.                     if (!file.exists()) file.createNewFile();
  35.                     BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
  36.                     for (int i = 0; i < amount; i++) {
  37.                         bw.write("String "+(i+1)+": ");
  38.                         System.out.print("String "+(i+1)+": ");
  39.                         for (int ff = 0; ff < slength; ff++) {
  40.                             if (type == 3) {
  41.                                 int r = (int) Math.floor((Math.random()*94));
  42.                                 System.out.print(""+rand_c[r]);
  43.                                 bw.write(""+rand_c[r]);
  44.                             } else if (type == 2) {
  45.                                 int r = (int) Math.floor((Math.random()*57));
  46.                                 System.out.print(""+rand_c[r]);
  47.                                 bw.write(""+rand_c[r]);
  48.                             } else if (type == 1) {
  49.                                 int r = (int) Math.floor((Math.random()*47));
  50.                                 System.out.print(""+rand_c[r]);
  51.                                 bw.write(""+rand_c[r]);
  52.                             } else {
  53.                                 int r = (int) Math.floor((Math.random()*94));
  54.                                 System.out.print(""+rand_c[r]);
  55.                                 bw.write(""+rand_c[r]);
  56.                             }
  57.                         }
  58.                         System.out.print("\n");
  59.                         bw.newLine();
  60.                     }
  61.                     bw.close();
  62.                     System.out.println("Written Strings to File: "+file);
  63.                     break;
  64.  
  65.                 case "no":case "n":case "false":case "f":case "0":
  66.                     if (type != 1 && type != 2 && type != 3) {
  67.                         System.out.println("Defaulted to mode: All");
  68.                     }
  69.                     for (int i = 0; i < amount; i++) {
  70.                         System.out.print("String "+(i+1)+": ");
  71.                         for (int ff = 0; ff < slength; ff++) {
  72.                             if (type == 3) {
  73.                                 System.out.print(""+rand_c[(int) Math.floor((Math.random()*94))]);
  74.                             } else if (type == 2) {
  75.                                 System.out.print(""+rand_c[(int) Math.floor((Math.random()*57))]);
  76.                             } else if (type == 1) {
  77.                                 System.out.print(""+rand_c[(int) Math.floor((Math.random()*47))]);
  78.                             } else {
  79.                                 System.out.print(""+rand_c[(int) Math.floor((Math.random()*94))]);
  80.                             }
  81.                         }
  82.                         System.out.print("\n");
  83.                     }
  84.                     break;
  85.  
  86.                 default:
  87.                     System.out.println("Invalid Choice.");
  88.                     main(args);
  89.                     break;
  90.             }
  91.             System.out.print("\n");
  92.         } catch (IOException e) {
  93.             e.printStackTrace();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement