2

I have been struggling with the following problem:

I am trying to print the below output using nested for loops and two dimensional arrays.

int[][] outputArray = {
    {1,2,3,4,5,6,7,8,9,10},
    {11,12,13,14,15,16,17,18,19,20},
    {21,22,23,24,25,26,27,28,29,30},
    {31,32,33,34,35,36,37,38,39,40},
    {41,42,43,44,45,46,47,48,49,50},
    {51,52,53,54,55,56,57,58,59,60},
    {61,62,63,64,65,66,67,68,69,70},
    {71,72,73,74,75,76,77,78,79,80}
};

Here is my code for the array which seems to be correct:

public ExerciseTwo() {
    myArray1 = new int[8][10];

    for (int i = 0; i < myArray1.length; i++) {
        for (int j = 0; j < myArray1[i].length; j++) {
            myArray1[i][j] = (i * myArray1[i].length) + j + 1;
        }// end inner loop
    }// end outer loop
}// end constructor

Now I am having several issues with the nested loop below:

public void printArrayStatement() {
    System.out.print("int[][] outputArray = {");

    for (int i = 0; i < myArray1.length; i++) {
        if (myArray1.length >= 1)
            // I am trying to remove the initial comma here but my
            // logic is wrong. It is printing 1 first on each line.
            System.out.print("\n" + "{" + myArray1[0][0]); 
        for (int j = 0; j < myArray1[i].length; j++) {
            System.out.print("," + myArray1[i][j]);
        }
    }
    System.out.println("};");
}// end method

I also can't seem to figure out how to get the }, at the end of each line. I think an if statement is necessary but I can't figure out the code!

4 Answers 4

2

The following code is working as required:

System.out.println("int[][] outputArray = {");   //int[][] outputArray = {
for (int i = 0; i < myArray1.length; i++) {
    System.out.print("{");                       //{
    int j;
    for (j = 0; j < myArray1[i].length - 1; j++) {
        //1, 2,...9,    i.e not last one.
        System.out.print(myArray1[i][j] + ", "); //1, 2,...9, then terminate
        // Not used if to check for last one
        // because it would increase time complexity
    }
    System.out.print(myArray1[i][j] + "}");      //10}
    if (i != myArray1.length - 1) {
        System.out.println(", ");                //, only if it is not last one
    }
}
System.out.println("\n}");

Output:

int[][] outputArray = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, 
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, 
{51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, 
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, 
{71, 72, 73, 74, 75, 76, 77, 78, 79, 80}
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please explain what is happening in your code? Why do you need two print statements that contain (myArray1[i][j])? Should that not make have the array elements print twice?
Tried to explain it with comments @macattack. Tell me if you still have any doubt.
Thanks. That makes sense to me now.
0

It looks like you're creating a complex bit of code that can be solved simply... print the comma if you're not at the end, and don't print it if you're at the end

for (int i = 0; i < myArray1.length; i++) {
  for (int j = 0; j < myArray1[i].length; j++) {
    // here print out myArray1[i][j]
    if (j != myArray1[i].length - 1) {
      // here print out comma if j is not == myArray1[i].length - 1
    }
  }
  System.out.println();
}

Another option is to use java.util.Arrays.toString(...)

I'm not sure why you are trying to print the { and } symbols. Are you sure that you need them?

5 Comments

Thanks for the response. Yeah I need the brackets. The output will copied into another method to initialize an array so the output has to be exact.
@macattack: this still strikes me as very strange. Your other method would work much better if you passed the actual array itself and not some String representation that only makes sense to a compiler. Color me very confused.
I used your if statement and I added the brackets using separate print statements as mentioned above and it is working now.
It's a unique situation. It was required by my prof. We have to test the accuracy of the printed output by copying and pasting the output into another method.
@macattack: then that's a very good reason to do something -- because the boss or instructor wants it done! I retract my questioning of the motivation behind this code.
0

Your inner loop is starting from zero:

   for (int j = 0; j < myArray1[i].length; j++) {

You should have

   for (int j = 1; j < myArray1[i].length; j++) {

As for the closing brace, just print it after the inner loop, but still inside the outer loop:

  System.out.println("}");
}
System.out.println("};");

5 Comments

Please elaborate why the inner loop should start at 1. I must be confused on what the OP is asking.
To skip the first item which was already explicitly printed inside the if block above it.
There is still an issue with the if statement. It is printing 1 as the first element in each line instead of increasing by 10. For example, line two should begin with 11, line 3 with 21 and so on...
Also, originally I had myArray1.length; in the outer loop. You changed it to myArray1[i].length; - why did you make that change? I did it and I am getting an ArrayIndexOutOfBoundsException.
So print myArray[i][0]. Also check myArray[i].length >= 1. Btw I never mention your outer loop conditions.
0

After the inner loop we print '}' and we check to see if it just printed the last line, if not then we need to print comma also.

Here is the code:

for (int i = 0; i < myArray1.length; i++) {
    if (myArray1.length >= 1)
        System.out.print("\n" + "{" + myArray1[0][0]);
    for (int j = 0; j < myArray1[i].length; j++) {
        System.out.print("," + myArray1[i][j]);
    }

    // Print ',' if we are not at the end.
    System.out.print("}"+(i==myArray1.length-1?"":",")); 
}
System.out.println("\n};");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.