As a challenge to my beginner programming ability I thought it would be fun to see if I could code a simple brute force password thing. So I’ve started writing an application which generates, given a value for the length of the string, every alphanumeric permutation it could take. However, as I am a complete programming newbie I am having troubles.
Firstly, despite having imported java.lang.Math, I’m getting errors saying cannot find symbol: pow. I managed to fix this by writing out in full java.lang.Math.pow(); when I use the function instead, but why that works but import does not is beyond me.
Secondly, regardless of the length input, after inputting I get the runtime error:
aaException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 98
at combination.main(combination.java:53)
Which suggests that in line 53:
current[j] = alphanum[((int)current[j])+1];
I’m apparently trying to access index 98 in either current[] or alphanum[]?
Which as far as I can see shouldn’t be happening…
I’m fairly stumped by this development. Anyway, here’s my code:
//48-57 65-90 97-122
import java.util.Scanner;
import java.lang.Math;
public class combination {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Alphanum will be an array of chars: the lowercase letters of the alphabet, the uppercase, and the numbers 0-9.
char[] alphanum = new char[62];
//Set indexes 0-25 as lowercase a-z, and indexes 26-51 as uppercase A-Z, using ascii conversion.
for (int i=0; i<26; i++) {
alphanum[i] = (char)(i+97);
alphanum[i+26] = (char)(i+65);
}
//Set indexes 51-61 as 0-9.
for (int i=0; i<10; i++) {
alphanum[i+52] = (char)(i+48);
}
//Take in variable for length.
System.out.print("Enter length: ");
int length = in.nextInt();
//Current will be an array of chars: it will hold the current permutation being generated.
char[] current = new char[length];
//Set all indexes in current to "a" by default, and print this string as the first permutation.
for (int i=0; i<length; i++) {
current[i] = alphanum[0];
System.out.print(current[i]);
}
//power will be a temporary double, used to calculate the number of iterations needed, as the pow function works with doubles.
double power = (java.lang.Math.pow(62.00, ((double)length)));
//Convert power to an integer, iterations, and subtract 1 because one iteration was already printed previously.
int iterations = ((int)power)-1;
/*The loop works like this. The rightmost char is checked, and if it is the maximum value of the idex
it is reverted to idex 0 again and the index value of the char to the left of it is increased by 1,
if it is not the maximum then it is just increased by 1. This is iterated the right number of times such
that every alphanumeric permutation of that length has been returned.*/
for (int i=0; i<iterations; i++) {
for (int j=(length-1); j>=0; j--) {
if ((j!=0) && (((int)current[j])==122)) {
current[j] = alphanum[0];
current[j-1] = alphanum[((int)current[j-1])+1];
} else if (j!=0) {
current[j] = alphanum[((int)current[j])+1];
} else {
System.out.println("This has looped too many times. Something is wrong.");
}
}
//At the end of each iteration, print the string.
for (int l=0; l<length; l++) {
System.out.print(current[l]);
}
}
}
}
I’d be really thankful for any help or insight you could offer. ^_^
Your
alphanumarray has size 62, and the meaning of((int)current[j-1])+1is 98 (> 62).The int value of char ‘a’ is 97.