Print two arrays on same line

just1han85

I'm sure this will be a simple question but I can't seem to find the right answer. I have two distinct String arrays, each with data and each have exactly the same array length (21). I'm simply trying to loop through both indexes and print index value 0 for array 1 and index value 0 for array 2 on the same line and so forth.

Here's what I have. I have one array called weightToOunce and another called weightArray. The problem with this code is it's based on the length of both arrays so each index value within the array is printed the length of the array (21 times in this case).

I've tried moving the print statement out of the loop and declaring an int i and int j variable before the loop but I haven't been successful there either.

public static void weightArrayLooper() {
    String[] weightToOunce = Project1.setWeightPercentageToOunce();
    for (int i = 0;i<weightArray.length;i++) {
        for (int j = 0;i<weightToOunce.length;j++) {
            System.out.println("Lb " + weightArray[i] +  " Oz " + weightToOunce[j]);
        }
    }
}
user12758604

Not sure if this is what you are trying to do, but you can use the same 'i' value to traverse both array!

public static void weightArrayLooper() {
    String[] weightToOunce = Project1.setWeightPercentageToOunce();
    for (int i = 0;i < weightArray.length;i++) {
            System.out.println("Lb " + weightArray[i] +  " Oz " + weightToOunce[i]);
        }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related