Results 1 to 3 of 3
- 12-20-2009, 01:12 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
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
- 12-20-2009, 02:34 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Don't play with ascii representations. Just do something like this:
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.Java Code:String lower = "abcd..."; String upper = "ABCD..."; String number = "0123..."; .... String random = ""; if (type == 2) random = lower + upper; .... int randomRange = random.length;
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?).
-
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
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Range within an Array
By End in forum New To JavaReplies: 6Last Post: 04-18-2009, 06:53 PM -
[SOLVED] special characters (ASCII) åäö
By AlejandroPe in forum New To JavaReplies: 8Last Post: 04-06-2009, 10:42 AM -
I need help with ascii characters
By Grandon in forum EclipseReplies: 17Last Post: 11-08-2008, 02:12 AM -
Printing ASCII values of characters
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks