
-----------------------------------
ImDennis
Wed Nov 23, 2011 4:19 pm

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

import java.util.Scanner;
import java.util.Scanner;
public class PasswordGenerator
{

	public static void main(String

-----------------------------------
Tony
Wed Nov 23, 2011 4:34 pm

RE:java password generator stuck
-----------------------------------
what kind of a loop are you using for "//start loop" ?

-----------------------------------
ImDennis
Wed Nov 23, 2011 4:36 pm

Re: RE:java password generator stuck
-----------------------------------
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 
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

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 :P

-----------------------------------
Tony
Wed Nov 23, 2011 4:42 pm

Re: RE:java password generator stuck
-----------------------------------
now im not getting random letters and numbers, just numbers :S its blowing my mind, 
But... you explicitly cast the choice into a number...

-----------------------------------
ImDennis
Wed Nov 23, 2011 4:46 pm

Re: RE:java password generator stuck
-----------------------------------
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
Wed Nov 23, 2011 5:00 pm

RE:java password generator stuck
-----------------------------------
emphasis mine

//generate random number 
randomHex = (int)(Math.random() * arrayHex.length);
System.out.println("your randomly generated password is " + randomHex); 

why do you even have arrayHex ?

-----------------------------------
ImDennis
Wed Nov 23, 2011 5:03 pm

Re: RE:java password generator stuck
-----------------------------------
emphasis mine

//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
Wed Nov 23, 2011 5:29 pm

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);
[/code]
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;
[/code]
Is this what you were expecting?

-----------------------------------
ImDennis
Wed Nov 23, 2011 6:57 pm

Re: 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 :P 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
Wed Nov 23, 2011 7:03 pm

RE:java password generator stuck
-----------------------------------
right, but a style of code like
[code]
if ( length == 6 ) {
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
   randomLetter();
} else ...
[/code]
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.

-----------------------------------
ImDennis
Wed Nov 23, 2011 7:26 pm

Re: RE:java password generator stuck
-----------------------------------
right, but a style of code like


hmmm, i guess your right how about something like this?

import java.util.Scanner;

		
	 //create arrays holding hex chars and secure chars
  char

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
Wed Nov 23, 2011 7:39 pm

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
[code]
for(int i = 0; i < someVariable; i++) { ...
[/code]

-----------------------------------
ImDennis
Wed Nov 23, 2011 7:51 pm

Re: 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


:D well i got it kinda, so heres what i got 
  //start loop
  	for(int i= 0; i