ATAX
ATAX

Reputation: 13

Using printf() to format output as a grid

I'm trying to grid an output using the printf() statement.

The Strings are generated via a 2D array that is iterated with two for loops

example:

    String[][] arr =
    {
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"}
    }

    for(int i = 0; i < arr.length; i++)
    {
       for(int j = 0; j < arr[0].length; j++)
       {
          System.out.printf("%s", arr[i][j]);
       }
    }

I basically want the output just as it appears in the code (as a 5x5 grid) but the tutorials on formatting are not very easy to follow and most do not deal with this sort of format.

Ideally, the output should look like this:

    * 2 1 * 1
    * 2 1 * 1
    * 2 1 * 1
    * 2 1 * 1
    * 2 1 * 1

My question is, besides how would I do this using the printf() characters, is will I come across any problems using these inside a nested for-loop? Also, I understand where the %s comes from, but any further symbol usage is beyond me. Could someone explain why they would use the symbol and characters they are using for this?

Thank you very much.

Upvotes: 1

Views: 6055

Answers (5)

Adrian M
Adrian M

Reputation: 7

I used to make it like this:

for(int i=0;i<matrix.length;i++){
    for(int j=0;j<matrix[i].length;j++){
        System.out.printf("%5d", matrix[i][j]);
    }
    System.out.println(" ");
}

Upvotes: 0

Scott Wierschem
Scott Wierschem

Reputation: 1

Now that Java 8 has been released, you can use the following:

System.out.printf("%s", Grid.print(width, height, this::showCell));

You'll need to add this as a separate formatter function:

public String showCell(Integer x, Integer y) {
    return arr[x][y];
}

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

String[][] arr =
    {
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"},
      {"*", "2", "1", "*", "1"}
    }

for(int i=0 ; i<arr.length ; i++){

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

         System.out.printf("%s ", arr[i][j]);

   }

     System.out.prinln();

}

Upvotes: 0

Jeff Ferland
Jeff Ferland

Reputation: 18312

Reference for printf() types in Java: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Code:

    for(int i = 0; i < arr.length; i++) {
       for(int j = 0; j < arr[0].length; j++) {
        //System.out.printf("%s", arr[i][j]);
          System.out.printf("%s ", arr[i][j]);
        //The added space will pad the end after each character
       }
       System.out.println();
       //After each pass printing all the characters of your array,
       //this will add a line return.
    }

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213371

You need to add a System.out.println(); after your inner loop ends to print your next row in a newline: -

for(int i = 0; i < arr.length; i++)
{
   for(int j = 0; j < arr[0].length; j++)
   {
      System.out.printf("%s ", arr[i][j]);
   }
   System.out.println();
}

For more details on various format specifier see this link: -

Upvotes: 2

Related Questions