0
public static void displaySummary(int totalShipment,
                                  String[] nameOfBus,
                                  int[][] storageBox)
{
    int counter;

    //Display the Summary Report              
    System.out.println("\nSummary\n- - - - - - - - - - - -");
    for(counter = 0; counter < totalShipment; ++counter)
    {
        System.out.println("Shipment #" + (counter + 1)+  " - " + nameOfBus[counter]);
        System.out.print("\nXL - " + storageBox[0][counter] + ",");
        System.out.print("L - " + storageBox[1][counter] + ",");
        System.out.print("M - " + storageBox[2][counter] + ",");
        System.out.print("S - " + storageBox[3][counter]+"\n");
    }

    System.out.println("\nTotal Number of containers required for these shipments:\n");
    System.out.println("XL - " + totalXL);
    System.out.println("L - " + totalL);
    System.out.println("M - " + totalM);
    System.out.println("S - " + totalS);        
}

When I call the displaySummary from main, regardless of any number of shipments, only the values of the last turn of the loop gets printed... If only one shipment, the values will be printed. If two shipments, the values of the second turn of the loop get printed but the first doesn't....

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • 2
    I created a snippet on repl.it (https://repl.it/CcnM/2) to test it and as you can see there it is printing all iterations of the loop as expected. In addition, your code currently refers to variables (totalXL, totalL ...) which are not defined locally - are those static fields of the surrounding class? Please provide more of your code or change my snippet in a way we can reproduce your issue. – DAXaholic Jul 17 '16 at 06:08
  • hi DAXholic. I'm just new in Java. I am using BlueJ 3.17. I hope you can help me. this is the complete code... (https://repl.it/CcnM/5) Thanks. – user6599123 Jul 17 '16 at 14:13
  • Hi, thanks for that information, but please [edit] your question with the full code – OneCricketeer Jul 17 '16 at 14:58
  • It looks like Joop Engen's answer and my comment are applicable. Please read them both and apply the suggestions there – OneCricketeer Jul 17 '16 at 15:02
  • @cricket_007 where should I put it in the code (repl.it/CcnM/5)? I still get errors... – user6599123 Jul 17 '16 at 15:52
  • Please **stop** posting links to your code and edit the question... You are getting an error on that site because you have to rename to `public class Main` – OneCricketeer Jul 17 '16 at 15:55
  • Plus, the first comment here does what you want. Your edits are clearly addressed in the mentioned answer. You are only using one array, and updating it within the array of arrays – OneCricketeer Jul 17 '16 at 15:57
  • Thanks @cricket_007... though, I'm still figuring out how to solve this problem... but your help is appreciated... – user6599123 Jul 17 '16 at 16:04

2 Answers2

1

This is a typical beginner's error.

If you add several items in the following manner:

int n = 0;
int[] box = new int[4];

box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;

box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;

box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;

The error is, that the same object, the new int[] you created just once, was placed in storageBox[0], [1] and [2]. You overwrote the sama array, box[0..4] several times upto the last value.

So storageBox[0] == storageBox[2] and their array values are the same.

For every storageBox item you have to add a new int[4].

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    Good point. Related: https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list – OneCricketeer Jul 17 '16 at 09:26
0

I called displaySummary for 3 bus in the main below. And I assume it works fine. So could you tell us a bit more about about calling code or executing plateforme so that we reproduce bug

public class Principal {
    public static void main(String[] args) {
        String[] busNames = new String[] {"Bus1", "Bus2", "Bus3"};
        int[][] boxes = {{1, 2, 2},{1, 2, 1}, {1, 2, 2}, {1, 2, 1}};

        displaySummary(3, busNames, boxes);
    }

    public static void displaySummary(int totalShipment, String[] nameOfBus, int[][] storageBox) {
        int counter;
        //Display the Summary Report              
        System.out.println("\nSummary\n- - - - - - - - - - - -");
        for(counter = 0; counter < totalShipment; counter++)
        {
            System.out.println("\n Shipment #" + (counter + 1)+  " - " + nameOfBus[counter]);
            System.out.print(" XL = " + storageBox[0][counter] + ", ");
            System.out.print("L = " + storageBox[1][counter] + ", ");
            System.out.print("M = " + storageBox[2][counter] + ", ");
            System.out.print("S = " + storageBox[3][counter]+"\n");
        }
    }
}

Run :

Summary
- - - - - - - - - - - -

Shipment #1 - Bus1
XL = 1, L = 1, M = 1, S = 1

Shipment #2 - Bus2
XL = 2, L = 2, M = 2, S = 2

Shipment #3 - Bus3
XL = 2, L = 1, M = 2, S = 1

Edit : I just modified counter incrementation (couner++ rather than ++counter) and reporting format for my convenience.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
  • hi @cricket_007 I'm just new in Java. I am using BlueJ 3.17. I hope you can help me. this is the complete code... (repl.it/CcnM/5) Thanks. – user6599123 Jul 17 '16 at 14:16