0

Good morning!

The goal of my code is to generate all possible three character strings that include A-Z and 0-9. For example, I want all possibilities from AAA to 999.

Problem: My own code will only output up to A99. My 2nd if statement is not generating the next step, which is confounding me. I need the 2nd if statement to work so that the A in A99 will increase to BAA and keep going.

Here is my code:

{    
        public static void main(String args[]) {

        String[] AZ09 = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

        while (z < AZ09.length) {
        System.out.println(AZ09[x]+AZ09[y]+AZ09[z]);
        z++;

              if ((AZ09[y]+AZ09[z]).equals("99") {
                   System.out.println(AZ09[x]+AZ09[y]+AZ09[z]);
                   x++;
                   y = 0;
                   z = 0;
              }

              if (AZ09[z].equals("9")) {
                   System.out.println(AZ09[x]+AZ09[y]+AZ09[z]);
                   y++;
                   z = 0;
              }    
        }

Solution? Thoughts?

Please don't post as duplicate. I tried the .equals() method and still no dice

Thank you so much!

2 Answers2

0

You have several problems:

  1. you're comparing strings with ==, you need to use equals
  2. you check az09[z] == 9, after you set z to 0, so next IF will never be true

as suggestion, you can go another way, check this code:

public static void main(final String[] args)
{
    final long start = Long.valueOf("999", 36);
    final long end = Long.valueOf("AAA", 36);

    for (long i = start; i <= end; i++)
        System.out.println(Long.toString(i, 36));
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

Why not try the following code instead? (Your if-statements cut out too many possible outputs, which is why your output is so small)

public class Main {
    public static void main(String[] args) 
    {
     String[] array = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

     for (int i = 0 ;i<array.length; i++){
         for (int j = i ;j<array.length; j++){
             for (int k = j ;k<array.length; k++){
                 System.out.println(array[i] + " " + array[j] + " " + array[k]);
             } 
         } 
     }

}

}