Posted: Wed Nov 23, 2011 4:19 pm Post subject: java password generator stuck
so i seem to be stuck on my password generator, anyone got any ideas on how to limit the characters? like i got it to randomly choose but it doesn't limit it to users choice which would be 6
Quote:
import java.util.Scanner;
import java.util.Scanner;
public class PasswordGenerator
{
public static void main(String[] args)
{
// Create Scanner object
Scanner input = new Scanner(System.in);
// create variables
int userChoice;
int randomHex; //holds array
// Tell user what the program will do
System.out.println("This program will randomly generate a password for you");
//ask user how many characters they want (minimum 6, maximum 16)
System.out.println("please enter how many characters do you want in your password (minimum 6, maximum 16)? ");
userChoice = input.nextInt();
//create arrays holding hex chars and secure chars
char[] arrayHex = { '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','1','2','3','4','5','6','7','8','9'
,'=','+','-','$','#','@','*','/','^','~' };
//start loop
if(userChoice == 6)
{
//generate random number
randomHex = (int)(Math.random() * arrayHex.length);
System.out.print("your" + randomHex);
//stub
}//end for if
}
//end main
}
//end class
Sponsor Sponsor
Tony
Posted: Wed Nov 23, 2011 4:34 pm Post subject: RE:java password generator stuck
what kind of a loop are you using for "//start loop" ?
Posted: Wed Nov 23, 2011 4:36 pm Post subject: Re: RE:java password generator stuck
Tony @ Wed Nov 23, 2011 4:34 pm wrote:
what kind of a loop are you using for "//start loop" ?
i was gonna use if else loop pretty much for user choices between 6 - 12, i noticed i kept getting bugs and i fixed it up a bit, but now im not getting random letters and numbers, just numbers :S its blowing my mind, heres what i got
Quote:
import java.util.Scanner;
/**
* Program Name:D_A_PasswordGenerator.java
* Purpose: generate random password using A-Z and 1-9
* Coder:Denis Aleta
* Date:Nov 23, 2011
*/
import java.util.Scanner;
public class D_A_PasswordGenerator
{
public static void main(String[] args)
{
// Create Scanner object
Scanner input = new Scanner(System.in);
// create variables
int userChoice;
int randomHex; //holds array
// Tell user what the program will do
System.out.println("This program will randomly generate a password for you");
//ask user how many characters they want (minimum 6, maximum 16)
System.out.println("please enter how many characters do you want in your password (minimum 6, maximum 16)? ");
userChoice = input.nextInt();
//create arrays holding hex chars and secure chars
char[] arrayHex = { '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','1','2','3','4','5','6','7','8','9'
,'=','+','-','$','#','@','*','/','^','~' };
//start loop
if(userChoice == 12)
{
//generate random number
randomHex = (int)(Math.random() * arrayHex.length);
System.out.println("your randomly generated password is " + randomHex);
//stub
}//finish this next day
}
//end main
}
//end class
edit
----
by if else i meant like if choice -- 6 then print out randomly generated password with 6 randomly generated characters, if else 7 then same thing but 7
Tony
Posted: Wed Nov 23, 2011 4:42 pm Post subject: Re: RE:java password generator stuck
ImDennis @ Wed Nov 23, 2011 4:36 pm wrote:
now im not getting random letters and numbers, just numbers :S its blowing my mind,
But... you explicitly cast the choice into a number...
Posted: Wed Nov 23, 2011 4:46 pm Post subject: Re: RE:java password generator stuck
Tony @ Wed Nov 23, 2011 4:42 pm wrote:
ImDennis @ Wed Nov 23, 2011 4:36 pm wrote:
now im not getting random letters and numbers, just numbers :S its blowing my mind,
But... you explicitly cast the choice into a number...
yea because i asked the user to choose between 6 and 16, so if he choose 6 then randomly generate a 6 letter/number password so since its a number they're putting in for choice, wouldn't it also be int?
Tony
Posted: Wed Nov 23, 2011 5:00 pm Post subject: RE:java password generator stuck
emphasis mine
Quote:
//generate random number
randomHex = (int)(Math.random() * arrayHex.length);
System.out.println("your randomly generated password is " + randomHex);
Posted: Wed Nov 23, 2011 5:03 pm Post subject: Re: RE:java password generator stuck
Tony @ Wed Nov 23, 2011 5:00 pm wrote:
emphasis mine
Quote:
//generate random number
randomHex = (int)(Math.random() * arrayHex.length);
System.out.println("your randomly generated password is " + randomHex);
why do you even have arrayHex ?
arrayhex holds the values, so now you got me thinking, maybe i should make a seperate var for limit so like arrayHexLimit = 16;? and then put it so it randomly generates from arrayHex? but what i was thinking was that math.random would randomly generate from ArrayHex with the users choice?
Tony
Posted: Wed Nov 23, 2011 5:29 pm Post subject: RE:java password generator stuck
I suspect that you might be making some assumptions that do not hold. What happens in
okay can you walk me through this im just learning java, what it is, is essentially a random password generator with user input to choose # of characters, between 6 and 12, so what i was thinking of doing was an if else statement so if the user chooses 6 then it randomly generates 6 characters using the letters and numbers in array, then else if 7 choose 7 randomly and else if 8 generate 8 characters, you know what i mean?
Tony
Posted: Wed Nov 23, 2011 7:03 pm Post subject: RE:java password generator stuck
seems like a pretty bad idea. For one, you'd just have to type out a lot of the same thing, and it would be too easy to make a mistake somewhere.
It would be best to learn some kind of a looping mechanism, so that you can get that randomLetter once, and repeat that as many times as you want.
hmmm, i guess your right how about something like this?
Quote:
import java.util.Scanner;
//create arrays holding hex chars and secure chars
char[] arrayHex = { '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','1','2','3','4','5','6','7','8','9'
,'=','+','-','$','#','@','*','/','^','~' };
//start loop
for(int i = 6; i < 16; i++)
{
System.out.println("Your password is for" + i + is" + arrayHex[i]);
}
}
//end main
}
//end class
would that work and then it' it'll just add? but i guess that loop would contently go on till 16? right, could u tell me the suggested loop? so i can research it?
Tony
Posted: Wed Nov 23, 2011 7:39 pm Post subject: RE:java password generator stuck
You are moving in the right direction
A for-loop and using indexes of an array are exactly the parts that you need.
That loop goes up to 16 only because that's what you wrote that example as. It's just as valid to use variables
Posted: Wed Nov 23, 2011 7:51 pm Post subject: Re: RE:java password generator stuck
Tony @ Wed Nov 23, 2011 7:39 pm wrote:
You are moving in the right direction
A for-loop and using indexes of an array are exactly the parts that you need.
That loop goes up to 16 only because that's what you wrote that example as. It's just as valid to use variables
code:
for(int i = 0; i < someVariable; i++) { ...
well i got it kinda, so heres what i got
Quote:
//start loop
for(int i= 0; i<array.length; i++)
{
if (i == 6)
{
System.out.println("Your 6 digit randomly generated password is " + arrayHex );//generate for 6 digits,
}
so since its a for loop and an if inside, cant i do else for 7 8 9 10 11 12 13 14 15 16? also can you please tell me the command to limit the number of characters popping in each loop other then that i think i might have got it
Tony
Posted: Wed Nov 23, 2011 8:04 pm Post subject: RE:java password generator stuck
what? Your code seems to have regressed to something that doesn't make sense to me.
Posted: Wed Nov 23, 2011 8:10 pm Post subject: Re: RE:java password generator stuck
Tony @ Wed Nov 23, 2011 8:04 pm wrote:
what? Your code seems to have regressed to something that doesn't make sense to me.
okay let me try and explain my thinking to you and maybe u can see what im doing wrong, so i made a for loop right , and then inside that for loop i wanted to do if else (idk if its possible) but then i wanted to put it like if (i == 6)
{
System.out.println("Your" + i " digit randomly generated password is " + arrayHex );
}
so it would generate a password for i which = 6, u know what i mean? if not how do u recommend doin it because thats how i see it the random letters numbers and all that stuff appear but its not limited