Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:51:36+00:00 2026-05-27T09:51:36+00:00

As a challenge to my beginner programming ability I thought it would be fun

  • 0

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. ^_^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:51:36+00:00Added an answer on May 27, 2026 at 9:51 am

    Your alphanum array has size 62, and the meaning of ((int)current[j-1])+1 is 98 (> 62).

    The int value of char ‘a’ is 97.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Challenge: I have this code that fails to compile. Can you figure out what's
The challenge The shortest code by character count to generate a wave from the
The challenge The shortest code by character count, that will output musical notation based
The challenge The shortest code by character count to output a spider web with
The challenge The shortest code by character count that will generate a beehive from
The Challenge Guidelines for code-golf on SO The shortest code by character count to
The challenge The shortest code by character count that will output the numeric equivalent
The challenge: The shortest code by character count that will generate a series of
The challenge The shortest code by character count to draw an ASCII representation of
The Challenge The shortest code by character count that takes a single input integer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.