Computer Science Canada

java password generator stuck

Author:  ImDennis [ 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

Author:  Tony [ 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" ?

Author:  ImDennis [ 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 Razz

Author:  Tony [ 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...

Author:  ImDennis [ 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?

Author:  Tony [ 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);

why do you even have arrayHex ?

Author:  ImDennis [ 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?

Author:  Tony [ 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
code:

randomHex = (int)(Math.random() * arrayHex.length);

lets pretend that arrayHex holds 16 elements, and that Math.random() picks a random value of 0.42. Then the code evaluates to
code:

randomHex = (int)(0.42 * 16);
randomHex = (int)(6.72);
randomHex = 6;

Is this what you were expecting?

Author:  ImDennis [ Wed Nov 23, 2011 6:57 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 5:29 pm wrote:
I suspect that you might be making some assumptions that do not hold. What happens in
code:

randomHex = (int)(Math.random() * arrayHex.length);

lets pretend that arrayHex holds 16 elements, and that Math.random() picks a random value of 0.42. Then the code evaluates to
code:

randomHex = (int)(0.42 * 16);
randomHex = (int)(6.72);
randomHex = 6;

Is this what you were expecting?


okay can you walk me through this Razz 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?

Author:  Tony [ Wed Nov 23, 2011 7:03 pm ]
Post subject:  RE:java password generator stuck

right, but a style of code like
code:

if ( length == 6 ) {
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
} else ...

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.

Author:  ImDennis [ Wed Nov 23, 2011 7:26 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 7:03 pm wrote:
right, but a style of code like
code:

if ( length == 6 ) {
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
} else ...

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?

Author:  Tony [ Wed Nov 23, 2011 7:39 pm ]
Post subject:  RE:java password generator stuck

You are moving in the right direction Smile

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++) { ...

Author:  ImDennis [ 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 Smile

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++) { ...


Very Happy 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 Very Happy other then that i think i might have got it

Author:  Tony [ 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.

Author:  ImDennis [ 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 Razz the random letters numbers and all that stuff appear but its not limited Razz

Author:  Tony [ Wed Nov 23, 2011 8:19 pm ]
Post subject:  RE:java password generator stuck

try to reason about what your code does. In terms of for-loops, walk through each step that it takes. What is the value of i? What happens during this step? etc.

Are those steps consistent with what you would do, if you had to come up with some random characters on paper?

Author:  ImDennis [ Wed Nov 23, 2011 8:26 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 8:19 pm wrote:
try to reason about what your code does. In terms of for-loops, walk through each step that it takes. What is the value of i? What happens during this step? etc.

Are those steps consistent with what you would do, if you had to come up with some random characters on paper?

after re reading the for loop chapter, i made a few changes so the for statement goes no higher then userChoice which is "for(int i= 0; i<userChoice; i++)"






Quote:
//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
for(int i= 0; i<userChoice; i++)
{

if (i == 6)
{
System.out.println("Your" + " digit randomly generated password is " + arrayHex );//
}




am i kinda on the right track? so it starts with i being null(0, no value etc) anyway then i becomes userchoice (so whatever the user put in the number of characters for his I) and it adds ++ and then i went to the if statement, which was, if i (which is userchoice) is equal to 6 that it would print out the ur 6 digit randomly generated password is and then the 6 random characters, thats my mind set as of right now

Author:  Tony [ Wed Nov 23, 2011 8:32 pm ]
Post subject:  RE:java password generator stuck

so i the userChoice is 6, how many characters does your code print out right now, when you run it?

Author:  ImDennis [ Wed Nov 23, 2011 8:36 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 8:32 pm wrote:
so i the userChoice is 6, how many characters does your code print out right now, when you run it?

as of right now, i get nothing but when i change it back to what i had before

Quote:
for(int i= 0; i<arrayHex.length; i++)
{

if (i == 6)
{
System.out.println("Your 6 digit randomly generated password is " + arrayHex );
}


}


it spits out 11

Author:  Tony [ Wed Nov 23, 2011 8:38 pm ]
Post subject:  RE:java password generator stuck

so the next step is to figure out why that is so.

Author:  ImDennis [ Wed Nov 23, 2011 8:41 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 8:38 pm wrote:
so the next step is to figure out why that is so.


why it spits out 11 digits or why that other one didn't do anthing?

Author:  Tony [ Wed Nov 23, 2011 8:53 pm ]
Post subject:  RE:java password generator stuck

Figuring out both would give you more understanding of what you've done; though one at a time.

Author:  ImDennis [ Wed Nov 23, 2011 9:02 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 8:53 pm wrote:
Figuring out both would give you more understanding of what you've done; though one at a time.


well after really sitting and thinking about it, i figured how to stop the spitting out 11 and rather user choice, so my thought was make a new var for that controls, so make a maximum value which would = userchoice right? so it cant exceed what the user wants, does that make sense? i figured if i put it like that then it wont generate more characters then the user wanted or would i be able to use decimal format? to restrict it

Author:  Tony [ Wed Nov 23, 2011 9:07 pm ]
Post subject:  RE:java password generator stuck

that explanation is really difficult to follow. Put it into code, and see what it does.

Author:  ImDennis [ Wed Nov 23, 2011 9:16 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 9:07 pm wrote:
that explanation is really difficult to follow. Put it into code, and see what it does.


honestly, im so lost, ima just have to ask my teacher, i spent like 6 hours trying to figure this out but i cant seem to do it :/ i tried to even
Quote:
import java.text.DecimalFormat;
and make it limit characters within the loop by the users choice so like
Quote:

for(int i= 0; i<arrayHex.length; i++)
{
if (i==6)
{
DecimalFormat formatter = new DecimalFormat("#00000");
System.out.println("Your randomly generated password is " + arrayHex );//
break;
}

and then in the next if else statement within the loop it was add 1 more digit to the character limiter and so that it increases as the user increases, i guess im doomed with this x.x thanks for the help appreciate it but im too lost for my own good now, i dont even know where to start anymore

Author:  Tony [ Wed Nov 23, 2011 9:26 pm ]
Post subject:  RE:java password generator stuck

sometimes it's not a bad idea to just throw everything away, and start a new blank project. Don't think of it as admitting defeat -- you've learned some things, at the very minimum you've grinded out more experience in the process. Starting fresh with that new level of knowledge if often beneficial.

Another good idea would be to start with some basic case, and you can build up from there. E.g. if you can make a random password of length 1, then you can do that same process 6 times to get a random password of length 6 (and in general case, N times to get any length you want).

Author:  ImDennis [ Wed Nov 23, 2011 9:28 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 9:26 pm wrote:
sometimes it's not a bad idea to just throw everything away, and start a new blank project. Don't think of it as admitting defeat -- you've learned some things, at the very minimum you've grinded out more experience in the process. Starting fresh with that new level of knowledge if often beneficial.

Another good idea would be to start with some basic case, and you can build up from there. E.g. if you can make a random password of length 1, then you can do that same process 6 times to get a random password of length 6 (and in general case, N times to get any length you want).


thanks for the advice, ill ply around with array building and learn how to add to them Razz and yea, even though i didn't figure it out Razz i appreciate you not telling actually doing it for me or telling me the answers but having me think Razz

Author:  Tony [ Wed Nov 23, 2011 9:33 pm ]
Post subject:  RE:java password generator stuck

we try to encourage that here. It's much more beneficial in the long run.

- Get some rest (6 hours is a long time).
- Explain this problem to someone (anyone! http://compsci.ca/blog/rubber-ducks-help-best-with-computer-science/ )
- come back to give this another shot.

Author:  ImDennis [ Wed Nov 23, 2011 11:12 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Wed Nov 23, 2011 9:33 pm wrote:
we try to encourage that here. It's much more beneficial in the long run.

- Get some rest (6 hours is a long time).
- Explain this problem to someone (anyone! http://compsci.ca/blog/rubber-ducks-help-best-with-computer-science/ )
- come back to give this another shot.



ohhh tony it hit me while i was breaking, the array is broken down
Quote:
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'
,'=','+','-','$','#','@','*','/','^','~' };


theres a total of 71 elements (but since 0 is a number theres a total of 70 elements Very Happy )Very Happy so i randomly choose 6 arrays from that choice, and so on, it hit me, i was so focused on getting it to work that i forgot what that was Razz, ill report back with my progress!


edit
-------
so what i did was
add
Quote:
import java.util.Random;
, and then i did
Quote:

for(int i= 0; i<arrayHex.length; i++)
{
arrayHex[i] = (char) (Math.random() *69 + 1);
System.out.println("the password generated is " + arrayHex);
break;
}


so i put i = 0 because i want the array to start from first one,, and then the ++ to add on, the i did the arrayHex[i] to use the i as a base value of array 0, then the char to gather from the char array, then math.random to generate random number from 69 and add 1, does this seem like im on the right track? i also got break to stop the loop after it gets it

Author:  Velocity [ Thu Nov 24, 2011 1:41 pm ]
Post subject:  Re: java password generator stuck

why dont you just put your array in char form array of 1 .. 255, so it includes every single possible character.

Author:  Tony [ Thu Nov 24, 2011 2:00 pm ]
Post subject:  RE:java password generator stuck

because some of the characters (including those between 0 to 69 (why did you pick this range?)) are non-printable control characters.

Also, what's the point of having a loop, if it always breaks out and never does a second iteration?

Author:  ImDennis [ Thu Nov 24, 2011 4:10 pm ]
Post subject:  Re: RE:java password generator stuck

Velocity @ Thu Nov 24, 2011 1:41 pm wrote:
why dont you just put your array in char form array of 1 .. 255, so it includes every single possible character.


yea i didn't think of this, Very Happy i changed my stuff


Tony @ Thu Nov 24, 2011 2:00 pm wrote:
because some of the characters (including those between 0 to 69 (why did you pick this range?)) are non-printable control characters.

Also, what's the point of having a loop, if it always breaks out and never does a second iteration?

i was thinking cuz of the array size Razz but i changed it, so now i got it to randomly generate numbers, heres what i got so far, but i cant figure out why each character is on a separate line, any suggestions?

Quote:

//start loop

char [] passwordArray = new char[userChoice];

for(char i = 0; i < passwordArray.length; i++)
{
passwordArray[i] = (char)(Math.random() * 122 + 33);

System.out.print("your password is " + passwordArray[i]);

}//end for loop



Author:  Tony [ Thu Nov 24, 2011 4:19 pm ]
Post subject:  Re: RE:java password generator stuck

This is looking much better, you're almost there ImDennis!
ImDennis @ Thu Nov 24, 2011 4:10 pm wrote:
why each character is on a separate line, any suggestions?

Well, how many times does "your password is " appears in the output?

Author:  ImDennis [ Thu Nov 24, 2011 4:24 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Thu Nov 24, 2011 4:19 pm wrote:
This is looking much better, you're almost there ImDennis!
ImDennis @ Thu Nov 24, 2011 4:10 pm wrote:
why each character is on a separate line, any suggestions?

Well, how many times does "your password is " appears in the output?

it appears exact amount of times as user asked , but it breaks it down in seperate lines, so i figured it must be cuz system.out.println is within the loop right? so when i took it out i got an error cuz [i] doesnt work outside the loop, so hers what i got

Quote:
char [] passwordArray = new char[userChoice];

for(char i = 0; i < passwordArray.length; i++)
{
passwordArray[i] = (char)(Math.random() * 122 + 33);

}//end for loop

System.out.print("your password is " + passwordArray[i] );

Author:  Tony [ Thu Nov 24, 2011 4:43 pm ]
Post subject:  RE:java password generator stuck

Right, what is the value of i when you exit the loop?

Besides, what does passwordArray[i] (for any value of i) mean? Is that your entire password?

Author:  ImDennis [ Thu Nov 24, 2011 4:50 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Thu Nov 24, 2011 4:43 pm wrote:
Right, what is the value of i when you exit the loop?

Besides, what does passwordArray[i] (for any value of i) mean? Is that your entire password?


okay so i got " int userChoice = 0; " which gives the user a choice to enter the limit, then i got " char [] passwordArray = new char[userChoice];" which takes the char array and makes it into a newchar of userchoice (kinda like a limiter, so it goes up to how many the user wants), then i got " for(char i = 0; i < passwordArray.length; i++)
{passwordArray[i] = (char)(Math.random() * 122 + 33)

which generates the password, random chars step by step

Author:  Tony [ Thu Nov 24, 2011 5:00 pm ]
Post subject:  RE:java password generator stuck

that's right. Now, after you generate all of the characters, you'd have to figure out how to print it all.

Author:  ImDennis [ Thu Nov 24, 2011 5:02 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Thu Nov 24, 2011 5:00 pm wrote:
that's right. Now, after you generate all of the characters, you'd have to figure out how to print it all.


okay well is there any way you can tell me how i can print results of it outside the loop? cuz if i put it inside the loop like
Quote:
//start loop

char [] passwordArray = new char[userChoice];

for(char i = 0; i < passwordArray.length; i++)
{
passwordArray[i] = (char)(Math.random() * 122 + 33);

System.out.print("your password is " + passwordArray[i] );

}//end for loop


it starts each letter on a seperate line, could i do a while loop to have it print out? while it does the loop?

Author:  Tony [ Thu Nov 24, 2011 5:05 pm ]
Post subject:  RE:java password generator stuck

I said that you should print after the loop is over and you have the entire password generated.

Author:  ImDennis [ Thu Nov 24, 2011 5:09 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Thu Nov 24, 2011 5:05 pm wrote:
I said that you should print after the loop is over and you have the entire password generated.


so should i convert the char into a string and it print outside the loop after, does that make sense to u?

Author:  Tony [ Thu Nov 24, 2011 5:58 pm ]
Post subject:  RE:java password generator stuck

as long as it's all of the chars, and not just a single char (your statement is ambiguous), that would work.

alternatively, print (and println) do support
Quote:

public void println(char[] x)

definition.

Author:  ImDennis [ Sun Nov 27, 2011 11:56 am ]
Post subject:  Re: java password generator stuck

oh sorry forgot to upload the code, after a bit of struggling i managed to get the code working to my approval and satisfaction Very Happy if u want u can edit it or make it better

Quote:
/**
* Program Name:D_A_PasswordGenerator.java
* Purpose: generate random secure password to be used!
* 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);
// Tell user what the program will do
System.out.println("This program will randomly generate a password for you");
// create variables
int userChoice = 0;
//ask user how many characters they want (minimum 6, maximum 16)
System.out.print("\nplease enter how many characters do you want in your password (minimum 6, maximum 16)? ");
userChoice = input.nextInt();
//make sure that user input is valid
if (userChoice < 6 || userChoice > 16)
{
System.out.println("not a valid input");
return;
}
//loop
char [] passwordArray = new char[userChoice];
for(char i = 0; i < passwordArray.length; i++)
{
passwordArray[i] = (char)(Math.random() * 122 + 33);
}//end for loop
//new loop to generate random password
String randomPassword = ""; //used to hold the random password
for(int i = 0; i != userChoice; ++i)
{
randomPassword = randomPassword + passwordArray [i]; //connect password array with random password
}
int iterations = (int)(Math.random()*25 + 1); //randomly generate a number to trick the user to thinking the program works
//print out results
System.out.println("\nyour password is: " + randomPassword);
System.out.println("it took a total of " + iterations +" iterations"+ " to get your new password, which is " + randomPassword);
}
//end main
}
//end class

Author:  Tony [ Sun Nov 27, 2011 12:32 pm ]
Post subject:  RE:java password generator stuck

Quote:

//randomly generate a number to trick the user to thinking the program works

lol, what?

Author:  ImDennis [ Sun Nov 27, 2011 12:56 pm ]
Post subject:  Re: RE:java password generator stuck

Tony @ Sun Nov 27, 2011 12:32 pm wrote:
Quote:

//randomly generate a number to trick the user to thinking the program works

lol, what?


instead of doing actual number of times to generate the password without certain ASCIIs i got lazy and just put a random generator to make it look like it actually takes try to generate Razz


: