Hey guys, I recently completed a program for my boss which simulates a brute force hacker in that it hacks a password by trying every possible combination of ascii characters until it finally discovers the password. I'm new to Java and I drew up a logical, straight forward way of doing it with lots of code reuse as I couldn't see any way of completing this class with lots of nested for loop methods. My boss told me to look into java recursion to speed up development time, but i'm not really sure what this consists of.. Can anyone give me any advice or elementary knowledge about this? I have also attached the aforementioned class if you're interested or may know how I could improve it with recursion.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package brute.force.password;


import java.util.Arrays;

public class BruteForcePassword {

    public int counter;
    boolean check;
    public String Password;
    public int passLength;
    public char[] checkPass;
    public char[] inPass;
    public char[] ascii;
    public String Builder;




    public BruteForcePassword(String Pass) {

        //Builds the ascii array
        char x;       
        for(x=' '; x<='~';  x++ ) 
        {  
            Builder += x;
        }
        ascii = Builder.toCharArray();

        Password = Pass;
        inPass = Password.toCharArray();
        passLength = inPass.length;
        checkPass = new char[passLength];
    }
   public boolean getBoolean(){
           return check;

   }
    public String getCurrentPass()
    {
        return checkPass.toString();
    }
    public String getCounter()
    {
       return String.valueOf(counter); 
    }
    public String check() {

        plusScanner();

        if (check == true) {
            return "Password Hacked!";
        } else {
            return "Could not hack password!";
        }

    }

    public void plusScanner() {

        int i;
        int j;
        int k;
        int l;
        int m;
        int n;
        int o;
        int p;
        int q;
        int r;
        int s;
        int t;
        int u;
        int v;
        int w;
        int x;

        /*
         * 1 Char
         */

        for (i = 0; i <= 98; i++) {

            checkPass[0] = ascii[i];
            counter++;
            if (Arrays.equals(inPass, checkPass)) {
                check = true;
                return;

            }


        }
        /*
         * 2 Char
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;

            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                if (Arrays.equals(inPass, checkPass)) {
                    check = true;
                    return;

                }
            }

        }

        /*
         * 3 Char
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    if (Arrays.equals(inPass, checkPass)) {
                        check = true;
                        return;

                    }
                }
            }
        }


        /*
         * 4 Char
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        if (Arrays.equals(inPass, checkPass)) {
                            check = true;
                            return;

                        }
                    }

                }
            }
        }
        /*
         * 5 Char
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            if (Arrays.equals(inPass, checkPass)) {
                                check = true;
                                return;

                            }
                        }
                    }

                }
            }
        }
        /*
         * 6 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                if (Arrays.equals(inPass, checkPass)) {
                                    check = true;
                                    return;


                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 7 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    if (Arrays.equals(inPass, checkPass)) {
                                        check = true;
                                        return;

                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 8 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        if (Arrays.equals(inPass, checkPass)) {
                                            check = true;
                                            return;

                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 9 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            if (Arrays.equals(inPass, checkPass)) {
                                                check = true;
                                                return;

                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 10 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                if (Arrays.equals(inPass, checkPass)) {
                                                    check = true;
                                                    return;

                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 11 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <= 98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    if (Arrays.equals(inPass, checkPass)) {
                                                        check = true;
                                                        return;

                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 12 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <= 98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    for (t = 0; t <= 98; t++) {
                                                        checkPass[11] = ascii[t];
                                                        counter++;
                                                        if (Arrays.equals(inPass, checkPass)) {
                                                            check = true;
                                                            return;

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 13 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];

                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <= 98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    for (t = 0; t <= 98; t++) {
                                                        checkPass[11] = ascii[t];
                                                        counter++;
                                                        for (u = 0; u <= 98; u++) {
                                                            checkPass[12]= ascii[u];
                                                            counter++;
                                                            if (Arrays.equals(inPass, checkPass)) {
                                                                check = true;
                                                                return;

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }

         /*
         * 14 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <= 98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    for (t = 0; t <= 98; t++) {
                                                        checkPass[11] = ascii[t];
                                                        counter++;
                                                        for (u = 0; u <= 98; u++) {
                                                            checkPass[12] = ascii[u];
                                                            for(v=0; v<=98; v++){
                                                                checkPass[13] = ascii[t];
                                                            counter++;
                                                            if (Arrays.equals(inPass, checkPass)) {
                                                                check = true;
                                                                return;

                                                            }
                                                        }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        /*
         * 15 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <= 98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    for (t = 0; t <= 98; t++) {
                                                        checkPass[11] = ascii[t];
                                                        counter++;
                                                        for (u = 0; u <= 98; u++) {
                                                            checkPass[12] = ascii[u];
                                                            for(v=0; v<=98; v++){
                                                                checkPass[13] = ascii[t];
                                                                for(w=0; w<=98; w++)
                                                                {
                                                                    checkPass[14] = ascii[w];
                                                            counter++;
                                                            if (Arrays.equals(inPass, checkPass)) {
                                                                check = true;
                                                                return;

                                                            }
                                                                }
                                                        }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
         /*
         * 16 Chars
         */
        for (i = 0; i <= 98; i++) {
            checkPass[0] = ascii[i];
            counter++;
            for (j = 0; j <= 98; j++) {
                checkPass[1] = ascii[j];
                counter++;
                for (k = 0; k <= 98; k++) {
                    checkPass[2] = ascii[k];
                    counter++;
                    for (l = 0; l <= 98; l++) {
                        checkPass[3] = ascii[l];
                        counter++;
                        for (m = 0; m <= 98; m++) {
                            checkPass[4] = ascii[m];
                            counter++;
                            for (n = 0; n <= 98; n++) {
                                checkPass[5] = ascii[n];
                                counter++;
                                for (o = 0; o <= 98; o++) {
                                    checkPass[6] = ascii[o];
                                    counter++;
                                    for (p = 0; p <= 98; p++) {
                                        checkPass[7] = ascii[p];
                                        counter++;
                                        for (q = 0; q <= 98; q++) {
                                            checkPass[8] = ascii[q];
                                            counter++;
                                            for (r = 0; r <= 98; r++) {
                                                checkPass[9] = ascii[r];
                                                counter++;
                                                for (s = 0; s <=98; s++) {
                                                    checkPass[10] = ascii[s];
                                                    counter++;
                                                    for (t = 0; t <= 98; t++) {
                                                        checkPass[11] = ascii[t];
                                                        counter++;
                                                        for (u = 0; u <= 98; u++) {
                                                            checkPass[12] = ascii[u];
                                                            for(v=0; v<=98; v++){
                                                                checkPass[13] = ascii[t];
                                                                for(w=0; w<=98; w++)
                                                                {
                                                                    checkPass[14] = ascii[w];
                                                                    for(x=0; x<=98; x++){
                                                                        checkPass[15]= ascii[x];
                                                            counter++;
                                                            if (Arrays.equals(inPass, checkPass)) {
                                                                check = true;
                                                                return;

                                                            }
                                                                    }
                                                                }
                                                        }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
    }
}
Member Avatar for Vesmir

Try this site http://introcs.cs.princeton.edu/java/23recursion/ for some basic info on recursion in Java.
With recursion you will be able to condense your code considerably, instead of having a bunch of loops you will be able to implement a function that calls itself repeatedly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.