Results 1 to 3 of 3
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default Randomly generate a range of Ascii Characters

    In this program, I am randomly generating a password allowing the user to select one of the following options:

    1) Lowercase letters
    2) Lowercase and uppercase
    3) Lowercase, Uppercase, and Numbers
    4) Lowercase, Uppercase, Numbers, and Symbols
    5) Quit (no password)

    I've set them up in different loops and the first, fourth, and 5th loops work. This is because the ascii characters needed are all within a range (ex: 65-90). For the second and third loop, the ascii characters needed aren't next to each other. So for the second loop, I need ascii characters 65-90 and 97-122. The code I have now I thought would work but it still generates some of the ones from 91-96. Once I figure out the second loop, I will know how to do the third. Here's my code...

    Java Code:
    /*
    *  Jamison Hyman
    *  12/19/09
    *  This program generates a random password.
    */
    
    import java.util.Scanner;
    import java.util.Random;
    import java.lang.*;
    import java.nio.charset.*;
    import java.util.Arrays;
    
    public class passwords {
    
       public static void main(String[] args)
       
       {
    
       	
     Scanner in = new Scanner(System.in);  	
     
     
    
      
     int userSelection;  	
     int userLength;  	
     int randNum;
     int randNum2;
     int randNum3;
     int randNum4;
       	 
       	 
    System.out.println("                Password Generator Menu               ");
    System.out.println();
    System.out.println("******************************************************");
    System.out.println("* [1] Lowercase Letters                              *");
    System.out.println("* [2] Lowercase & Uppercase Letters                  *");
    System.out.println("* [3] Lowercase, Uppercase, and Numbers              *");
    System.out.println("* [4] Lowercase, Uppercase, Numbers, and Punctuation *");  	
    System.out.println("* [5] Quit                                           *");
    System.out.println("******************************************************");
    System.out.println();	
    System.out.print("Enter Selection (1-5): ");
    userSelection = in.nextInt();  
    if (userSelection == 5)
    {
    System.out.println();
    System.exit(0);
    } 	
    System.out.println();
    System.out.print("Password Length (1-14): ");
    userLength = in.nextInt();	
    System.out.println();
    
    
    if (userSelection == 1)
    {
    System.out.print("Password: ");
    randNum2 = (int)((Math.random() * 26) + 97);
    
    for(int i=0; i<userLength; i++){
    				randNum2 = (int)((Math.random() * 26) + 97);
                   System.out.print((char)randNum2);
              }
    }
    
    [B][I]if (userSelection == 2)
    {
    	System.out.print("Password: ");
    
    for(int i=0; i<userLength; i++){
    	randNum2 = (int)((Math.random() * 52) + 65);
    	
    		if (randNum2 >= 91)
    			{
    				
    				System.out.print((char)(randNum2 + 7));
    			}
    				
    			
                   System.out.print((char)randNum2);
              }
    	
    }[/I][/B]
    
    if (userSelection == 3)
    {
    	System.out.print("Password: ");
    randNum2 = (int)((Math.random() * 9) + 48);
    
    for(int i = 0; i<userLength; i++){
    				randNum2 = (int)((Math.random() * 9) + 48);
                   System.out.print((char)randNum2);
              }	
    	
    }
    
    if (userSelection == 4)
    {
    	System.out.print("Password: ");
    randNum2 = (int)((Math.random() * 88) + 33);
    
    for(int i=0; i<userLength; i++){
    				randNum2 = (int)((Math.random() * 88) + 33);
                   System.out.print((char)randNum2);
              }	
    		
    }
    System.out.println();
       	
       	
       	
       	
         	
    }
    }


    And the output! :(

    Java Code:
                    Password Generator Menu
    
    ******************************************************
    * [1] Lowercase Letters                              *
    * [2] Lowercase & Uppercase Letters                  *
    * [3] Lowercase, Uppercase, and Numbers              *
    * [4] Lowercase, Uppercase, Numbers, and Punctuation *
    * [5] Quit                                           *
    ******************************************************
    
    Enter Selection (1-5): 2
    
    Password Length (1-14): 14
    
    Password: g`slngg`XAohohmfTngFd]mf
    Press any key to continue...
    Last edited by Fubarable; 12-20-2009 at 01:42 AM. Reason: code tags added

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Don't play with ascii representations. Just do something like this:

    Java Code:
    String lower = "abcd...";
    String upper = "ABCD...";
    String number = "0123...";
    ....
    
    String random = "";
    
    if (type == 2)
       random = lower + upper;
    ....
    
    int randomRange = random.length;
    Now you can create a loop to get indexes. Once you get an index you access the character from the random string using the charAt(...) method.

    Or if you wanted to get fancy maybe you could use the Collections.shuffle(...), that way you don't need to worry about duplicates (or maybe duplicates are acceptable?).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I had something along the same lines using enums,...
    Java Code:
    enum PasswordChars {
       LOWER_CASE_LETTERS("abcdefghijklmnopqrstuvwxyz"), 
       UPPER_CASE_LETTERS("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
       NUMBERS("0123456789"), 
       PUNCTUATION("!@#$%^&*.");
    
       private String validChars;
    
       private PasswordChars(String validChars) {
          this.validChars = validChars;
       }
    
       public String getValidChars() {
          return validChars;
       }
    
       public static String getValidCharStrings(PasswordChars... pwChars) {
          StringBuilder validCharsSB = new StringBuilder();
          for (PasswordChars passwordChar : pwChars) {
             validCharsSB.append(passwordChar.getValidChars());
          }
          return validCharsSB.toString();
       }
    }

    which can be used like so:
    Java Code:
    import java.util.Random;
    
    public class Fu2 {
       private static final int PASSWORD_LENGTH = 10;
    
       public static void main(String[] args) {
          PasswordChars[] chosenPasswordChars = {
             PasswordChars.LOWER_CASE_LETTERS, PasswordChars.NUMBERS
          };
          String validCharStrings = PasswordChars.getValidCharStrings(chosenPasswordChars);
          Random random = new Random();
    
          StringBuilder password = new StringBuilder();
          for (int i = 0; i < PASSWORD_LENGTH; i++) {
             password.append(validCharStrings.charAt(random.nextInt(validCharStrings.length())));
          }
          System.out.println(password.toString());
    
       }
    }

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Range within an Array
    By End in forum New To Java
    Replies: 6
    Last Post: 04-18-2009, 06:53 PM
  3. [SOLVED] special characters (ASCII)
    By AlejandroPe in forum New To Java
    Replies: 8
    Last Post: 04-06-2009, 10:42 AM
  4. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 02:12 AM
  5. Printing ASCII values of characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •