出界。试图无缘无故地访问索引98

时间:2011-12-10 19:24:12

标签: java arrays import indexoutofboundsexception

作为对初学者编程能力的挑战,我认为看看我是否可以编写一个简单的暴力密码事件会很有趣。所以我开始编写一个应用程序,它给出字符串长度的值,它可以生成每个字母数字排列。但是,由于我是一个完整的编程新手,我遇到了麻烦。

首先,尽管导入了java.lang.Math,但我收到的错误是找不到符号:pow。我设法通过写出完整的java.lang.Math.pow();当我使用该函数时,但为什么这样可行,但导入不是我的。

其次,无论输入长度如何,输入后我都会得到运行时错误:

aaException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 98
at combination.main(combination.java:53)

这表明第53行:

current[j] = alphanum[((int)current[j])+1];

我显然试图在current []或alphanum []中访问索引98? 据我所见,不应该发生......

我对这一发展感到非常难过。无论如何,这是我的代码:

//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]);
            }
        }
    }
}

我非常感谢您提供的任何帮助或见解。 ^ _ ^

2 个答案:

答案 0 :(得分:4)

您的alphanum数组的大小为62,((int)current[j-1])+1的含义为98(&gt; 62)。

char'a'的int值是97。

答案 1 :(得分:1)

  

我显然试图在current []或者中访问索引98   alphanum []?据我所见,不应该发生......

这是完全可能的,因为您尝试在某个索引处访问alphanum中的元素,其中索引是从当前数组的内容派生的。我建议你在不同的步骤打印出这些数组的内容,你会很快发现你的代码在哪里行为不像你想象的那样

current[j] = alphanum[((int)current[j])+1];

在这里,您尝试访问

int index = ((int)current[j])+1;
current[j] = alphanum[index];

其中index似乎是98