-2

This is a weight loading program using a greedy approach to fill wagons in a caravan. I am trying to concatenate Integer elements from an array into a string output where it must follow this pattern.

```
(wagon capacity) -> (weights of packages in wagon) = (total weight of the wagon)
100 –> (70, 25) = 95
200 -> (100, 90) = 190
140 –> (78, 60) = 138
600 –> (600) = 600
```

right now my code sorts(descending) my capacities and my weights and it adds them to a onBoard variable, when i try to print, it shows the capacities correctly but it is not concatenating my weights, while at the same time reusing the first weight. Here is my code, any feedback will be appreciated.

```
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Integer[] cartCapacity = {100, 200, 140, 600};
    Integer[] cargoTons = {50, 60, 70, 100, 50, 41, 25, 78, 90, 41, 600};

    /*Desired output
     *(wagon capacity) -> (weights of packages in wagon) = (total weight of the wagon)
     * 100 –> (70, 25) = 95
     * 200 -> (100, 90) = 190
     * 140 –> (78, 60) = 138
     * 600 –> (600) = 600
     * 
     * Current output
     * 600->{600}
     * 200->{600}
     * 140->{600}
     * 100->{600}
     */

    //Sorting Carts
    Arrays.sort(cartCapacity, Collections.reverseOrder());
    //Sorting package tonnage
    Arrays.sort(cargoTons, Collections.reverseOrder());


    //After sorting carts
    System.out.println("The carts capacities are: ");
    for (int i = 0; i < cartCapacity.length; i++) {
        System.out.println(cartCapacity[i]);
    }


    System.out.println();


    //After sorting package tonnage
    System.out.println("The packages weights are: ");
    for (int i = 0, k = 0; i < cargoTons.length; i++) {
        System.out.println(cargoTons[i]);
    }


    int cartOnBoard = 0;
    int cartIdx = 0;
    String cartElements = "";
    while (cartIdx < cartCapacity.length) {
        for (int i = 0; i < cargoTons.length; i++) {
            if (cartOnBoard + cargoTons[i] <= cartCapacity[cartIdx]) {
                cartOnBoard += cargoTons[i];
                cartIdx++;
                cartElements += Integer.toString(cartOnBoard);
                System.out.println(Integer.toString(cartCapacity[i]) + "->{" + cartElements + "}");
            } 
            else
                System.out.println(Integer.toString(cartCapacity[i]) + "->{" + cartElements + "}");
        }
    }
    }
```
J. Murray
  • 1,460
  • 11
  • 19

1 Answers1

1

There are multiple problems, here is fixed loop with comments:

int cartIdx = 0;
while (cartIdx < cartCapacity.length) {
    int cartOnBoard = 0; // this should be initialized for every cart
    String cartElements = ""; // this should be initialized for every cart
    for (int i = 0; i < cargoTons.length; i++) {
        if ((cartOnBoard + cargoTons[i]) <= cartCapacity[cartIdx] && cargoTons[i] > 0) {
            cartOnBoard += cargoTons[i];
            cartElements += Integer.toString(cargoTons[i]) + ",";
            cargoTons[i] = -1; // you need to mark used cargos
        }
    }
    System.out.println(Integer.toString(cartCapacity[cartIdx]) + "->{" + cartElements + "}"); // print one line per cart, not per every cargo
    cartIdx++;
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57