1

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.

1
  • add System.out.println() after the nested for Commented Nov 5, 2012 at 17:57

5 Answers 5

2

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: -

Sign up to request clarification or add additional context in comments.

3 Comments

Change System.out.printf("%s", arr[i][j]); to System.out.printf("%s ", arr[i][j]); (space between values)
Wow, I had a hunch but I thought I could insert new lines with \n after so many characters. Thank you.
Finally got around to choosing this as the accepted answer...2 years later. Sorry about that.
0

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.
    }

Comments

0
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();

}

Comments

0

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];
}

Comments

0

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(" ");
}

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.