Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Stupid Programmer at work!

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    226

    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?


  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255
    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?

  3. #3
    Senior Member
    Join Date
    May 2003
    Posts
    226
    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.


  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  5. #5
    Senior Member
    Join Date
    May 2003
    Posts
    226
    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

  6. #6
    Banned
    Join Date
    Sep 2004
    Posts
    305
    You want to print out every possible combination of an 8 char password using which characters exactly?

    Thanks,
    jag

  7. #7
    Senior Member
    Join Date
    May 2003
    Posts
    226
    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

  8. #8
    Banned
    Join Date
    Sep 2004
    Posts
    305
    I'm bored right now so I'll take a quick stab at it, can you write down EXACTLY what characters?

  9. #9
    Senior Member
    Join Date
    May 2003
    Posts
    226
    well, i am looking for an algorithm....

  10. #10
    Banned
    Join Date
    Sep 2004
    Posts
    305
    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
  •