Search code examples
javafor-loopnested-loops

Nested loop pattern return null


I am trying to make id for items. But after A6 B6.., it set to null.

The minimum number of item is 0 and max is 36. The pattern should be

A1-A6 B1-B6 C1-C6 D1-D6 E1-E6 F1-F6

For example if I have 32 items. The id should stop at F2.

My code:

    public void printMenu() {

        System.out.println("\nMenu:");
        System.out.printf("%s%9s%14s%8s\n", "Item#", "Item", "Price", "Qty");

        char letter = 'A';
        for (int i = 0; i < stock.length; ++i) {
            for (int j = 1; j < 7; j++) {
                stock[i].setId(letter + "" + j);
                i++;
            }
            letter++;
        }

  for (int i = 0; i < stock.length; ++i) {
        System.out.printf("%s%15s%13s%8s\n" , stock[i].getId() ,stock[i].getDescription(),
                (stock[i].getPrice()),stock[i].getQuantity());
        }
    }

The output:

Menu:
    Item#     Item         Price     Qty
    A1        Gummies         -1.0      -1
    A2          Chips         -1.0      -1
    A3        Raisins         -1.0      -1
    A4       Pretzels         -1.0      -1
    A5         Cookie         -1.0      -1
    A6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    B1        Gummies         -1.0      -1
    B2          Chips         -1.0      -1
    B3        Raisins         -1.0      -1
    B4       Pretzels         -1.0      -1
    B5         Cookie         -1.0      -1
    B6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    C1        Gummies         -1.0      -1
    C2          Chips         -1.0      -1
    C3        Raisins         -1.0      -1
    C4       Pretzels         -1.0      -1
    C5         Cookie         -1.0      -1
    C6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    D1        Gummies         -1.0      -1
    D2          Chips         -1.0      -1
    D3        Raisins         -1.0      -1
    D4       Pretzels         -1.0      -1
    D5         Cookie         -1.0      -1
    D6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    E1        Gummies         -1.0      -1
    E2          Chips         -1.0      -1
    E3        Raisins         -1.0      -1
    E4       Pretzels         -1.0      -1
    E5         Cookie         -1.0      -1
    E6        Gummies         -1.0      -1

Solution

  • Edit - Explanation with Example :
    A detailed Explanation.
    The output of this loop, if you see closely, the value of i is skipped from 5-7, so array at position 6 was untouched. Resulting in the id property not getting set. Look at line 5 A6 - 7 B1 after A6, index of 7 was updated and not 6.

     int stock[] = new int[40];
     char letter = 'A';
    
     // After j loop exists, here i again gets incremented.
     for (int i = 0; i < stock.length; ++i) {
        for (int j = 1; j < 7; j++) {
            System.out.println(i + " " + letter + j);
            i++; // This increments the value of i
        }
        letter++;
     }
    
    0 A1
    1 A2
    2 A3
    3 A4
    4 A5
    5 A6
    7 B1
    8 B2
    9 B3
    10 B4
    11 B5
    12 B6
    14 C1
    15 C2
    16 C3
    17 C4
    18 C5
    19 C6
    21 D1
    22 D2
    23 D3
    24 D4
    25 D5
    26 D6
    28 E1
    29 E2
    30 E3
    31 E4
    32 E5
    33 E6
    35 F1
    36 F2
    37 F3
    38 F4
    39 F5
    40 F6
    

    Original Answer :

    The value of i in the nested loop, is incremented twice skipping one value entirely whenever j loop ends.

       for (int i = 0; i < stock.length; ++i) { // After j loop exists, here i again gets incremented.
            for (int j = 1; j < 7; j++) {
                stock[i].setId(letter + "" + j);
                i++; // This increments the value of i 
            }
            letter++;
        }
    

    You can remove the incrementing of i in i loop.

     for (int i = 0; i < stock.length; ) {
            for (int j = 1; j < 7; j++) {
                stock[i].setId(letter + "" + j);
                i++; // This increments the value of i 
            }
            letter++;
        }
    

    or use a while outside

    int i = 0;
    while(i < stock.length) {
            for (int j = 1; j < 7; j++) {
                stock[i].setId(letter + "" + j);
                i++; // This increments the value of i 
            }
            letter++;
        }