Student
Student

Reputation: 387

Format print correctly

Trying to print out this statement correctly, it is alittle complicated because I need a "|" immediately after a column from my 2d array is printed out.

    System.out.println(toprow);
    System.out.println(botrow);
    System.out.println(line2);
    for(row=0;row<22;row++)
      {
      System.out.printf("%02d%s ", row,"|");
        for(col=0;col<32;col++)
          System.out.printf("%s",mapicons[row][col]);
          System.out.printf("%s", "|");
        System.out.println();
      }

This is what is being printed out:

  | 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3|
  | 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2|
  |----------------------------------------------------------------|
00|                                                                 |
01|                                                                 |
02|      *                                                          |
03|                                                                 |
04|                *                                                |
05|                                                                 |
06|                                                                 |
07|                    *                                            |
08|                                                            #    |
09|                                                                 |
10|                          *                                      |
11|                                                                 |
12|                                                                 |
13|                                                                 |
14|                        *                                        |
15|                                          *                      |
16|                                                                 |
17|                                                                 |
18|                                  *                              |
19|                                                                 |
20|                                                                 |
21|                                                                 |

Can't figure out how to line up the "|" at the end. If I add it to my first printf statement within the col for statement it will print a "|" for each column. So im not sure how to get rid of the space it automatically takes between the two print statements.

Upvotes: 0

Views: 39

Answers (1)

rgettman
rgettman

Reputation: 178303

It looks like you have an extraneous space character when printing the left side of each row:

//                       v-- here
System.out.printf("%02d%s ", row,"|");

Remove it; everything else looks like it's printing fine.

System.out.printf("%02d%s", row,"|");

Upvotes: 3

Related Questions