1

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

1 Answers1

3

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++;
    }
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • I'm not convinced that the loop is the problem. If you only print out the value, you'll see A1-F6 printed correctly. – Makoto Dec 02 '16 at 04:40
  • I don't understand the reason for down votes. If you closely analyze the loops, after initializing an entire character it always skips one place in the actual array. This is the reason for null in the output. – Kishore Bandi Dec 02 '16 at 04:46
  • I Agree with @BandiKishore. The issue is with the loop, why the downvotes. Is something wrong with the explanation? –  Dec 02 '16 at 04:48
  • The explanation was less clear. I admit my error in only looking at the output. This is correct. – Makoto Dec 02 '16 at 04:49
  • @swis Welcome! Can you accept the answer and close out the question? – Kishore Bandi Dec 02 '16 at 04:58
  • java.lang.ArrayIndexOutOfBoundsException: –  Dec 02 '16 at 05:33
  • @swis I guess you don't actually have that length. Change your `for (int j = 1; j < 7; j++)` to `for (int j = 1; j < 7 && i < stock.length; j++)` – Kishore Bandi Dec 02 '16 at 05:42