2

I have a problem with increasing number and character combinations. What I want is increase from 001 to ZZZ

Example: 001, 002,..., 999, 00A,..., 00Z, 0AA,..., ZZZ

My code look like this:

int numberA = 1000;
int numberB = 1024;
int numberC = 1025;

/*
 * Some formulae here
 */

System.out.println(numberA);
//Result: 00A
System.out.println(numberB);
//Result: 00Z
System.out.println(numberC);
//Result: 0A0

Are there any fomulae to solve this problem?

deHaar
  • 17,687
  • 10
  • 38
  • 51
th3l0n3r4n93r
  • 105
  • 1
  • 8
  • based on what do you want to switch to characters? would the numbers be always 3-digit? why would you want to do it (curious)? – Naman Nov 23 '18 at 12:31
  • Number is auto increase from 1 -> 18576 (001 -> ZZZ). Yes. Those output always have 3 digits. I have a school project that need to store lots of items in the warehouse, each item must have different "code" within one day. My format is yymmdd-XXX with XXX is the number above. I want to extend as much as possible the range of those number. – th3l0n3r4n93r Nov 23 '18 at 12:41
  • Please check this https://stackoverflow.com/questions/5091355/increase-string-value – Deedar Ali Brohi Nov 23 '18 at 12:43
  • Do check this out https://stackoverflow.com/questions/28981669/generate-new-number-combination-alphanumeric – Ojasvi Bhargava Nov 23 '18 at 12:46

3 Answers3

5

Maybe the following will help you get started ;-)

final Integer radix = 36; // that's 0-9 A-Z
final Double limit = Math.pow(radix.doubleValue(), 3.0 /* max number of 'chars' */);
Stream.iterate(0, i -> i+1)
        .map(i -> Integer.toString(i, radix))
        .map(s -> String.format("000%S", s)
                        .substring(s.length())) // leading 0, uppercase
        .limit(limit.longValue())
        .forEach(System.out::println);

Or simply:

String radix36 = Integer.toString(yourIntThatYouCanIncrement, 36);

Of course if you require the 00#-format (leading zeros and uppercase) you need to apply that functions too. Holgers comment already contains a short variant of it to combine uppercase/leading zeros:

String formatted = String.format("000%S", radix36)
                         .substring(radix36.length());
Roland
  • 22,259
  • 4
  • 57
  • 84
3

You can format your number as a base-36 number (you want to use 36 different digits: 0 - 9 = 10 digits + A - Z = 26 digits).

To get it exactly in the format you want (upper-case, with leading zeroes):

String s = Integer.toString(numberA, 36).toUpperCase();
String result = String.format("%3s", s).replace(' ', '0');
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    Or `String s = Integer.toString(numberA, 36); String result = String.format("000%S", s) .substring(s.length());`… – Holger Nov 23 '18 at 13:07
1

Here is code that will print 001 to zzz sequence.

public static void main(String[] args){
        String result="";
        int i=0;    
        while(!result.equals("ZZZ")){
            String s = Integer.toString(i, 36).toUpperCase();
             result = String.format("%3s", s).replace(' ', '0');
            System.out.println("\t "+result);
            i++;
        }
}
samji
  • 31
  • 6