CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: For Loop Help

  1. #1
    Join Date
    Feb 2011
    Posts
    4

    For Loop Help

    Hi
    I been trying to figure out this problem for last few days but cannot find the answer.
    I am trying to use nested for loops to try and create a 6 character string using letters/numbers from a array. I need it so that it starts off with one character and then adds one more etc. Then need it to stop when it finds the word that has been inputted.

    Example is
    a
    b
    ..z
    then
    aa
    ab
    ac
    ...az

    or even

    a
    z
    aa
    ba
    za etc

    thanks

  2. #2
    Join Date
    Oct 2010
    Posts
    60

    Re: For Loop Help

    I'm not quite sure what you're trying to do. Are you trying to make a random 6 character string from the characters in the array? You could do:
    Code:
    public static void main(String[] args) {
    	char[] myCharacters = {'a', 'b', 'c', 'd', 'e'};
    	Random r = new Random();
    	String s = "";
    	for(int i = 0; i < 6; i++) {
    		int nextIndex = r.nextInt(myCharacters.length);
    		char nextChar = myCharacters[nextIndex];
    		s += nextChar;
    	}
    	System.out.println(s);
    }

  3. #3
    Join Date
    Feb 2003
    Location
    Sunny Glasgow!
    Posts
    258

    Re: For Loop Help

    try to get it outputting:
    aa
    ab
    ac
    ...
    az
    ba
    ...
    bz

    once you've got that have a think about how in the first loop you could export a blank before the initaial 'a'

  4. #4
    Join Date
    Feb 2011
    Posts
    4

    Re: For Loop Help

    thanks guys
    but got it working a very messy way

    Code:
    char[] letters = { 
              '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', '0','1','2','3','4','5','6','7','8','9',
          };
          boolean found = false;
          char[] guess = new char[7];
          int pos = 0, i = 0;
          String pass, guess1, guess2, guess3;
           loop:
    
               
      while (found == false) {
           
             
                for (int a = 0; a<36; a++){
    
    
                guess[0] = letters[a];
    
    
                pass = String.valueOf(guess);
    
                SHA1.SHA1(pass);
    
               System.out.println(pass);
    
                 if (SHA1.hash == null ? hex == null : SHA1.hash.equals(hex)) {
                 System.out.println("Found " + pass);
                 found = true;
    
                    }}
                    
                 for (int a = 0; a<36; a++){
    
                 for (int b = 0; b<36; b++){
              
                 guess[0] = letters[a];
                  guess[1] = letters[b];
    
    
         pass = String.valueOf(guess);
    
          SHA1.SHA1(pass);
    
          System.out.println(pass);
    
           if (SHA1.hash == null ? hex == null : SHA1.hash.equals(hex)) {
             System.out.println("Found " + pass);
             found = true;
            
                     }}}
    etc

    now Im having another issue, the words it creates dont have the same SHA1 hash as the inputed word even though they are both the same word strings.

  5. #5
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: For Loop Help

    Actually it's not that messy, the original solution for the problem also compares hashed values instead of strings. As for the problem check the strings you compare and the SHA method you use. If it still fails the problem is in the SHA method, then use manual implementation instead:
    Code:
    public static String sha1Enc(String str){
        char s[] = null;
        try{
            MessageDigest md5 = MessageDigest.getInstance("SHA");
            char alphabet[] = "0123456789abcdef".toCharArray();
            byte pwdBytes[] = md5.digest(str.getBytes());
            s = new char[pwdBytes.length<<1];
    
            for(int i=0, j=0; i<pwdBytes.length; i++){
                s[j++] = alphabet[(pwdBytes[i]&0xf0)>>4];
                s[j++] = alphabet[pwdBytes[i]&0xf];
            }
        }catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        }
        return new String(s);
    }
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  6. #6
    Join Date
    Feb 2011
    Posts
    4

    Re: For Loop Help

    thanks I figured it out, it was because I had a array of 6 and when I converted it to string when it had less then 6 chars it created a String with spaces in it.

    Whats the best way to end all 6 for loops when it finds a match?
    I tried putting a while found == false at the start but it keeps on doing the inner loops even after it finds a match even though it sets it to found = true once a match is found.

  7. #7
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: For Loop Help

    That happens because the expression in while is getting evaluated every time you iterate through it but not instantly after changing the value from the expression. So the loops you have inside the while are considered to be the same iteration pass.

    To stop immediately the execution process use the labeled break statement. The difference can be easily seen in this example:
    Code:
    public static void main(String[] args){
    
        boolean myvar;
        int i;
        
        myvar = true;
        i=0;
        
        while(myvar){
            for(i=0; i<21; i++){
                if(i==10){
                    myvar = false;
                }
            }        
        }
        System.out.println("i="+i); //prints out '21'
        
        
        myvar = true;
        i=0;
        
        mywhilelabel:while(myvar){
            for(i=0; i<21; i++){
                if(i==10){
                    myvar = false; //not even necessary since the 'break' will stop the loop before the while condition is evaluated
                    break mywhilelabel;
                }
            }
        }
        System.out.println("i="+i); //prints out '10'
    }
    Last edited by Xeel; February 27th, 2011 at 04:12 PM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  8. #8
    Join Date
    Feb 2011
    Posts
    4

    Re: For Loop Help

    edit

    fixed it thanks
    Last edited by nekromantik; February 27th, 2011 at 06:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured