java password generator stuck
Author |
Message |
Tony

|
Posted: 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? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Sponsor Sponsor

|
|
 |
ImDennis
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ImDennis
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ImDennis
|
Posted: 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? |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ImDennis
|
Posted: 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 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ImDennis
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ImDennis
|
|
|
|
 |
Tony

|
|
|
|
 |
ImDennis
|
Posted: 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 ) 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 , 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 |
|
|
|
|
 |
Velocity

|
Posted: 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. |
|
|
|
|
 |
|
|