-
December 22nd, 2004, 03:27 PM
#1
Senior Member
Stupid Programmer at work!
Code:
public class Test {
char[] array = {'a', 'b', 'c', 'd'};
public void run() {
for (int a=0; a<array.length; ++a) {
for (int b=0; b<array.length; ++b) {
for (int c=0; c<array.length; ++c) {
for (int d=0; d<array.length; ++d) {
System.out.println(array[a] + ""
+ array[b] + "" + array[c] + "" + array[d]);
}
}
}
}
}
public static void main(String[] args) {
Test test = new Test();
test.run();
}
}
Stupid programmer, that's me. I am coding a program to print out a 8 char password program, containing uppercase, lowercase alphabets, digits and special chars like @#!$%^. I have coded a prototype to test out my idea, it seems to work, but at this rate i will be coding 8 stupid for loops to get the job done. Is there any other way to optimize/simplify how it can be done?
-
December 22nd, 2004, 04:36 PM
#2
Just use one for loop and change the output based on an array, but first make sure your application makes logical sense.
Chris Shepherd
The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
\"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
Is your whole family retarded, or did they just catch it from you?
-
December 22nd, 2004, 04:43 PM
#3
Senior Member
Originally posted here by chsh
Just use one for loop and change the output based on an array, but first make sure your application makes logical sense.
I have testout... it seems that my codes doesn't really work well if i am using 8 char password. Something is wrong with the codes.
-
December 24th, 2004, 02:42 AM
#4
Is your program to randomly generate passwords containing the characters you specified? If it is, I would simply have the one for loop with a pseudo random number generator in it like so:
Code:
public class test
{
public static void main(String [] args)
{
char[] pwd = new char[8];
for(int x=0; x<8; x++)
{
do
{
int tmp = (System.random()*254)+1;
}
while(/*put in code to check that tmp is in correct range*/);
pwd[x] = (char)tmp;
}
System.out.println(tmp);
}
}
For the arguments to the while loop, look at an ascii table (http://www.asciitable.com I think) and find out the decimal ranges that you need to make sure that only certain characters are chosen. Hope I didn't spoil your fun or something, and I hope that's what you were wanting.
[edit]If you need to have a certain mix of characters, all you need to do is add in some simple checking code to either rerun the above code till it gets the mix (not very clever) or to keep a tally of what has been chosen so far (not so random) and attempt to "fix" the selection[/edit]
ac
-
December 24th, 2004, 07:48 AM
#5
Senior Member
Originally posted here by gothic_type
Is your program to randomly generate passwords containing the characters you specified? If it is, I would simply have the one for loop with a pseudo random number generator in it like so:
Code:
public class test
{
public static void main(String [] args)
{
char[] pwd = new char[8];
for(int x=0; x<8; x++)
{
do
{
int tmp = (System.random()*254)+1;
}
while(/*put in code to check that tmp is in correct range*/);
pwd[x] = (char)tmp;
}
System.out.println(tmp);
}
}
For the arguments to the while loop, look at an ascii table (http://www.asciitable.com I think) and find out the decimal ranges that you need to make sure that only certain characters are chosen. Hope I didn't spoil your fun or something, and I hope that's what you were wanting.
[edit]If you need to have a certain mix of characters, all you need to do is add in some simple checking code to either rerun the above code till it gets the mix (not very clever) or to keep a tally of what has been chosen so far (not so random) and attempt to "fix" the selection[/edit]
ac
well it is to generate all possible combinations of a 8 char password. So i believe there's a algorithm or something to follow, rather than 8 nested for loops to do the job
-
December 24th, 2004, 07:57 AM
#6
You want to print out every possible combination of an 8 char password using which characters exactly?
Thanks,
jag
-
December 24th, 2004, 08:01 AM
#7
Senior Member
Originally posted here by ;TT
You want to print out every possible combination of an 8 char password using which characters exactly?
Thanks,
jag
Yup. What i did was creating a char array to hold all the characters including alphabets(uppercase/lowercase), numeric digits, and punctuation.
My way of doing it was creating 8 nested for loop to run through the array. I am thinking is there any better solution to the problem
-
December 24th, 2004, 08:05 AM
#8
I'm bored right now so I'll take a quick stab at it, can you write down EXACTLY what characters?
-
December 24th, 2004, 08:08 AM
#9
Senior Member
well, i am looking for an algorithm....
-
December 24th, 2004, 08:13 AM
#10
Mmmhmmm.. I'm leaning towards what gothic said, you have to give a specifc range from which a character can be chosen using the ASCII table... from there you can have it create a list of every possible 8 character password there is. I don't know of any other way to get every possible 8 char possible password other than that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|