How to combine a one-dimensional array with a two-dimensional array

Viewed 82

I have an array of customer names:

String[] customers = {"Joe Dirt", "John Dirt", "Josh Dirt"};

I also have a two-dimensional array that has acct numbers/payments:

double[][] payments = {{1011,12,54,39},{1012,55,65,48},{1013,56,92,55}};

I need to match and print out the customer's name with their account and payments. The arrays are already sorted in order I just need to print out "Joe Dirt 1011, 12, 54, 55" and so on.. I'm trying to understand how to loop through the two-dimensional array.

This is what I have:

public static ArrayList<String> combineArray(String[] customers,
                                             double[][] payments) {

    ArrayList<String> customerPayments = new ArrayList<String>();

    String result = "";
    String custName = "";
    String acctInfo = "";

    for (String n : customers) {
        custName = n;
        for (double[] x : payments) {
            for (double y : x) {
                acctInfo = String.valueOf(y);
            }
        }
        result = custName + acctInfo;
        customerPayments.add(result);
    }
    return customerPayments;
}

It's printing out the customer names correctly, but printing out only the last value of the payment two-dimensional array. ex.

Joe Dirt55
John Dirt55
Josh Dirt55

What am I overlooking here?

3 Answers

You can do it for example:

String[] customers = {"Joe Dirt", "John Dirt", "Josh Dirt"};
double[][] payments = {{1011,12,54,39},{1012,55,65,48},{1013,56,92,55}};
for (int i = 0; i < customers.length; i ++) {
    System.out.println(customers[i] + Arrays.toString(payments[i]));
}

or, if you want to see output strings without brackets try to do it:

for (int i = 0; i < customers.length; i++) {
    System.out.println(customers[i] + " " +
            Arrays.toString(payments[i]).replaceAll("\\[|\\]", ""));
}

There are number of approaches you could take. First, I suggest you program to the List interface (instead of the ArrayList concrete type). Second, use an explicit index in the first array so you can track your position in the payments array. Third, a StringJoiner might be useful for combining your elements. Something like,

public static List<String> combineArray(String[] customers, double[][] payments) {
    List<String> customerPayments = new ArrayList<>();
    for (int i = 0; i < customers.length; i++) {
        StringJoiner sj = new StringJoiner(", ");
        for (double x : payments[i]) {
            sj.add(String.format("%.0f", x));
        }
        customerPayments.add(String.format("%s %s", customers[i], sj));
    }
    return customerPayments;
}

To make it easier to validate

public static void main(String[] args) {
    String[] customers = { "Joe Dirt", "John Dirt", "Josh Dirt" };
    double[][] payments = {
            { 1011, 12, 54, 39 },
            { 1012, 55, 65, 48 },
            { 1013, 56, 92, 55 }
    };
    combineArray(customers, payments).stream().forEach(System.out::println);
}

And I get

Joe Dirt 1011, 12, 54, 39
John Dirt 1012, 55, 65, 48
Josh Dirt 1013, 56, 92, 55
  1. use an index in the customers cycle so you can use it to get the corresponding payments
  2. you need to concatenate acctInfo, instead of replacing its value every time
  3. you need to clear acctInfo at the start of the customers cycle so it does not accumulate the payments of the previous customers

For example:

public static ArrayList<String> combineArray(String[] customers, double[][] payments) {

    ArrayList<String> customerPayments = new ArrayList<>();

    for (int i = 0; i < customers.length; i++) {
        String acctInfo = "";
        String custName = customers[i];
        for (double y : payments[i]) {
            acctInfo += String.valueOf(y);
        }
        String result = custName + acctInfo;
        customerPayments.add(result);
    }
    return customerPayments;
}
Related