I'm learning java atm and I am kinda stuck at the current task, that my trainer gave me yesterday...
The exercise is:
- Create a 4 digit password (Just as String variable). (Allowed are numbers between 0-9 and one ore more of these special chars: '!','#','%')
- Find a way to bruteforce the password, by going through all possibilities.
- Measure and output the time that it took for processing.
- Additionally, output how many "tries" it needed to find the password.
(My trainer said that I should use as less methods as possible, because I don't even now yet how to write methods or how to use classes, etc.)
I managed it to work, but at how it is now, it takes about 60ms to go through the loops. My trainer now told me, that I should try to make it process faster, so that it takes about 20ms minimum.
I already now what makes my code slow. Its because i ALWAYS go through ALL possibilities, add ALL OF THEM into an ArreyList and THEN I check, if the pw matches with one of those possibilities in the ArreyList. Waste of time. Now my goal is to go through the possibilities like i did before, but ONLY until the password is found. After that, it should brake the loop, so that it also stops adding the rest of the unnecessary combinations. I tried and tried, but now I decided to call for help :)
That's my code:
import java.util.ArrayList;
import java.util.Scanner;
public class trynew {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '#', '!', '%'};
System.out.println("\nPlease create 4 digit password. Allowed are numbers between 0-9 and following characters: #,!,%");
String passw = scn.nextLine();
ArrayList<String> check_it = new ArrayList<String>();
long start1 = System.currentTimeMillis();
for (int i = 0; i < digits.length; i++) {
for (int j = 0; j < digits.length; j++) {
for (int k = 0; k < digits.length; k++) {
for (int l = 0; l < digits.length; l++) {
check_it.add(digits[i] + "" + digits[j] + "" + digits[k] + "" + digits[l]);
}
}
}
}
long end1 = System.currentTimeMillis();
for (String sv : check_it) {
if (sv.equals(passw)) {
System.out.println("\nThe Password is: " + sv);
System.out.println("It took " + check_it.indexOf(sv) + " tries, to find the password.");
}
}
System.out.println("Process time was " + (end1 - start1) + " milliseconds.");
}
}
My approach was to make the loop like that:
for (int i = 0; i < digits.length; i++) {
for (int j = 0; j < digits.length; j++) {
for (int k = 0; k < digits.length; k++) {
for (int l = 0; l < digits.length; l++) {
if (!(digits[i] + "" + digits[j] + "" + digits[k] + "" + digits[l]).equals(passw)){
check_it.add(digits[i] + "" + digits[j] + "" + digits[k] + "" + digits[l]);
}
}
}
}
}
Then I tried using a while loop instead of the if, I tried setting booleans, etc. I also managed that it only adds combinations until pw is found, but somehow the processing time won't go down :/
The reason why I want to add the combos into check_it ArreyList is because otherwise, I wouldn't now how I get the numbers of tries it took, until pw is found...
Can someone help me please, or poke me at the right direction?! Thanks & Greets!