I basically have to create a method that takes in a string array and a 2D array consisting of three double arrays and puts the first string with the first array, second string with the second array, etc.

public static void printData(String[] c, double[][] d) {
    String cname = "";
    for (int i = 0; i < c.length; ++i) {
        cname = c[i];
        for (int row = 0; row < d.length; ++row) {
            for (int col = 0; col < d[0].length; ++col) {
                System.out.print(d[row][col] + " ");
            }
            System.out.println();
        }
    }
}

Printed out the array a few times

//String word = "";

    //for(int i = 0; i < c.length; ++i)
  //{
        //for(int row = 0; row < d.length; ++row)
    //{ 
        //System.out.println();
        //for(int col = 0; col < d[0].length; ++col)
        //{
            //word = c[i];
            //System.out.println(d[i][col]);
        //}
    
    //}
  //}

I did get to the point where I was able to print the city names out with the entire 2D array under them.

2

There are 2 best solutions below

0
Jacek Kaczmarek On

To achieve printing each string from the c array with its corresponding double array from the d 2D array on the same line, you should iterate through both arrays simultaneously. Here's how you can modify your method:

public static void printData(String[] c, double[][] d) {
for (int i = 0; i < c.length; ++i) {
    // Print the string from the c array
    System.out.print(c[i] + " ");

    // Print the corresponding double array from the d 2D array
    for (int col = 0; col < d[i].length; ++col) {
        System.out.print(d[i][col] + " ");
    }

    // Move to the next line for the next string and its corresponding double array
    System.out.println();
}

This modified method will print each string from the c array along with the values of the corresponding double array from the d 2D array on the same line.

0
WJS On

It is presumed that the string array is the same length as the number of rows of the double array.

  • iterate over the number of rows doing the following:
    • print the string value for given row.
    • then print the double values for that row.
    • then print a new line.
  • done iterating

You may need to pad the values with spaces to properly format it.