Create a unique four digit code from 35 characters
By my count, 0-9 + a-z gives 36 characters rather than 35.

For four digits like that, the number of combinations is \$36^4\$, which works out to 1,679,616.

As for the code itself, I think I'd do things just a little differently. What you really have is a 4-digit number in base 36. It's probably easiest to just use a normal number, and then use toString to generate the base-36 representation when you need it:

Integer counter;
String result = toString(counter, 36);


This does differ in one minor detail: it won't fill in leading zeros, so if it's shorter than 4 characters, you'll have to fill in the leading zeros yourself (still a pretty trivial operation though).

For the sake of security, I don't install Java on my machines, but there's a rough imitation of the basic idea, but in C++:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;deque&gt;
#include &lt;cassert&gt;

// This should be approximately equivalent to what you'd get from 
// Java's `toString`, at least for non-negative numbers:
std::string toString(unsigned value, unsigned base) {
    static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";

    assert(base &lt;= 36);

    std::deque&lt;char&gt; ret;

    while (value) {
        unsigned digit = value % base;
        value /= base;
        ret.push_front(digits[digit]);
    }
    return std::string(ret.begin(), ret.end());
}

// This calls toString, then pads the result out to 4 digits.
std::string paddedToString(unsigned value, unsigned base, unsigned num_digits) {
    std::string s = toString(value, base);

    // Add enough 0's to get to the desired width:
    return std::string(num_digits - s.size(), '0') + s;
}

// A quick test of the code.    
int main() { 
    // First we'll write the first few values:
    for (unsigned value = 0; value &lt; 256; value++) 
        std::cout &lt;&lt; paddedToString(value, 36, 4) &lt;&lt; "\t";

    std::cout &lt;&lt; "\n\n";

    // Then some values from around the middle of the range:
    unsigned base = 512 * 1024;

    for (unsigned i = 0; i &lt; 256; i++)
        std::cout &lt;&lt; paddedToString(i + base, 36, 4) &lt;&lt; "\t";
}