John Mac
John Mac

Reputation: 57

String Index Error

I keep getting an error that states my String index is out of range at String.charAt, PasswordGenerator.matchCharAt, and Driver.main. I don't know what that means exactly. Also my characters won't append to one line from the stringbuilder class that I already instantiated. I was wondering if maybe that was caused by the String index error or whether it was my fault.

public class Driver {

    public static void main(String[] args) {
        int length=0;
        int MaxNumber=100;
        StringBuilder password = new StringBuilder();

        do {
            if (PasswordGenerator.matchLength (length))
                System.out.println("The length of the character is " + length);
            length++;                                     // length is randomly picked
        } while (length < MaxNumber );   // or <100

        int index = 0;
        char f = 0;

        for (int d = 0; d < 127 || ; d++) {
            if  (PasswordGenerator.matchCharAt(f, index))
                d = (char) index;
            char aChar = (char)d;
            password.append(aChar);
            System.out.println("Password is: " + aChar);
            index++;

        }
    }
}

Upvotes: 0

Views: 176

Answers (1)

Roland Illig
Roland Illig

Reputation: 41617

You are getting the error since idx will vary between 0 and 127. The password from the PasswordGenerator is probably not that long. For example, before you ask whether there is a match at index 57, you must ask if 57 is less than the length of the password.

So your task is to guess the password that the generator saves? Then you should do this:

Get to know the length of the password.
For each index from 0 upto but excluding the length:
    Guess the character at that index.

Upvotes: 2

Related Questions