0

If I have n numbers and the maximum number is 6 and the maximum letter is F, what is the best way to do it.

A1 
A2
A3
A4
A5 
A6 
B1
B2 
etc..

int numList = 36; 

         char letter;

        for (int i=1; i<=36; ++i){
            int a = numList/6;
            letter = 'A';
          for (int j=0; j<= a; j++){
            System.out.println(letter +"" + i);
            letter++;
          } 
    }
4
  • 1
    What you have to do with it ? Commented Nov 30, 2016 at 4:43
  • 2
    The best way to do it is to just do it. Commented Nov 30, 2016 at 4:45
  • @Gendarme Hysterical +1 Commented Nov 30, 2016 at 4:45
  • @Gendarme Great +1 Commented Nov 30, 2016 at 4:47

4 Answers 4

2

In short:

public static void main(String[] args) {
        for (int i = 0; i < 6; i++) {
            char c = (char) (i + 65);
            for (int j = 1; j <= 6; j++) {
                System.out.println("" + c + j);
            }
        }
    }

In Long:

You should practice skill in problem solving. Comprehension those small problems will support you much in programming career.

The most important skill when solving problem is breaking apart small problem. Firstly, you see that the output is: A1 A2 A3 ... B1 B2 B3 .. It looks like you can break apart each string (ie: A1) to 2 part: the first part from A to F and the second part from 1 to 6. This is the well-known pattern for applying two nested loop. Here is the pseudocode:

public static void main(String[] args) {
    for (char c = A to F) {
        for (int j = 1; j <= 6; j++) {
            System.out.println("" + c + j);
        }
    }
}

You see that. the first loop is not true (grammar syntax). So we should learn new thing: char in Java is just an integer. (this is true in most languages such as C#, javascript ...) but not all. So I lookup at ascii table here. I see that A to F start from 65, 66, 67, ... So I can start the loop with 6 elements. Each element I add into 65. For example:

0 -> 65 -> A
1 -> 66 -> B
2 -> 67 -> C

And as you guess, it become my final code I have posted above. Hope this help you :)

Sign up to request clarification or add additional context in comments.

1 Comment

please view my updated answer. I think this will help you improve in thinking skill.
1

Change your code like this:

int numList = 36 / 6;
    char letter = 'A';
    for (int i = 1; i <= numList; ++i) {
        for (int j = 1; j <= numList; j++) {
            System.out.println(letter + "" + j);
        }
        letter++;
    }

Comments

1

This will be the code :

    int[] array_of_ints; // declare and add the values into it - 1-6
    String[] array_of_alphabtes; // declare and add the values into it - A-F

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

1 Comment

I like this answer because it makes explicit what is being used to construct each "digit" which the OP wants.
0

When you have a nested loop, the entire inner loop, along with the rest of the outer loop's body, is run every time the outer loop runs.

Here, every time you run the inner for, the body of the inner loop is executed 6 times, which means you print 6 lines.

So the way you've written it: the outer loop is executed 36 times, and each time the outer loop runs, the inner loop is executed 6 times. That means the inner loop is executed a total of 36*6 = 216 times, causing 216 lines to be printed. That isn't what you want. You want the outer loop to run only 6 times, so your first for needs to change.

Another feature of nested loops: the inner loop repeats faster than the outer loop. So in your case, when the first iteration of the outer loop runs, the inner loop executes 6 times. Then we go to the next iteration of the outer loop, and the inner loop executes another 6 times; only then do we execute the outer loop the third time, and so on.

According to your desired output, you want the digit to change faster than the letter. That means that the digit needs to be increased by 1 in the inner loop, and the letter needs to be increased in the outer loop. You're doing it backwards, though--you're increasing the letter in the inner loop, and the digit (i) in the outer loop.

I don't want to give you the code--I hope I've given you enough information that you can fix it yourself. Please try doing this before you look at the code that others gave you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.