Results 1 to 2 of 2
  1. #1
    cooldood is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Question Error in a simple script I am working on.

    I am making a simple utility where you specify a string, and the script guesses a string until it equals that of the specified string in the while statement, this case being the word "password".
    However when running the following code I get the error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 62
    at BruteForce.main(BruteForce.java:25)

    I am VERY new to Java, so I would like to understand why. Also, are there better ways to write this code, and shorter/more efficient ways? I want it to pick out 8 random characters from the 62 character string of all possible letters and numbers both capital and lowercase. I know it seems that I am a little newbie to write something like this, but I want the challenge!


    Java Code:
    import java.util.Arrays;
    import java.util.Random;
    
    public class BruteForce {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            char[] charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
            
    	  String passGuess = null;
           while (passGuess != "password"){
            int guessPass = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass1 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass2 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass3 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass4 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass5 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass6 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            int guessPass7 = 0 + (int)(Math.random() * ((charSet.length - 0) + 1));
            passGuess = charSet[guessPass] + "" + charSet[guessPass1] + "" + charSet[guessPass2] + "" + charSet[guessPass3] + "" + charSet[guessPass4] + "" + charSet[guessPass5] + "" + charSet[guessPass6] + "" + charSet[guessPass7] + "";
    		
           System.out.println(passGuess);
           }
        }
    }
    Any help would be great, thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Error in a simple script I am working on.

    Plug in the maximum value for Math.random() and see what array index you get back. Then you'll see why you're going out of bounds.

    Also, you will not want to use == or != for comparing Strings. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two *objects* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.

Similar Threads

  1. partially working script
    By burfodus in forum New To Java
    Replies: 2
    Last Post: 03-27-2012, 09:28 AM
  2. Script not working with IE7
    By sfphoto in forum New To Java
    Replies: 1
    Last Post: 12-13-2010, 05:08 PM
  3. Help java script error
    By ellybeth in forum New To Java
    Replies: 7
    Last Post: 08-20-2009, 05:23 AM
  4. JSP send mail script not working.
    By profuse in forum Java Applets
    Replies: 1
    Last Post: 05-27-2008, 06:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •