-1

How can I visualize the array that I created in the code with brackets? e.g for 3x3 array:

 [  ]    [  ]    [  ]   
 [  ]    [  ]    [  ]   
 [  ]    [  ]    [  ]  

I tried a very simply code but the columns were below the others

for (int i=0;i<Columns;i++){
       for (int j=0;j<Rows;j++){
          System.out.println("[  ]");
       }
   System.out.println(" ");
}
Michael Pnrs
  • 115
  • 11

3 Answers3

2
public class Main{

 public static void main(String []args){

    int Columns = 3,Rows = 3;
    for (int i=0;i<Columns;i++){

           for (int j=0;j<Rows;j++){
              System.out.print("[  ]");
              System.out.format("\t");
           }
       System.out.println(" ");
    }

 }

}

This code will work. Do not use System.out.println() instead use System.out.print().

for reference click this link. https://www.onlinegdb.com/B1950yw2V

import java.util.Formatter in your code.

Kashyap Neeraj
  • 154
  • 3
  • 14
2

Your biggest issue is that System.out.println("[ ]"); prints each string out to a new line.

To get the desired effect, try System.out.print("[ ]");, which prints each column without adding a carriage return.

int Columns = 3;
int Rows = 3;

for (int i=0;i<Columns;i++){
    for (int j=0;j<Rows;j++){
        System.out.print("[  ]");
    }
    System.out.println(" ");
}

Output:

[  ][  ][  ]   
[  ][  ][  ]   
[  ][  ][  ]  
bschreck
  • 183
  • 1
  • 9
0

Check this:

        StringBuilder sb = new StringBuilder();
        int Columns = 3, Rows = 3;
        for (int i = 0; i < Columns; i++) {
            for (int j = 0; j < Rows; j++) {
                sb.append("[  ]");
                if (j == Rows - 1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        System.out.println(sb.toString());