views:

35

answers:

1

Hi,

I have a bunch of data that I need to beautify and output. The current code I'm using is:

String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", string, int, int, int);

The resulting output from my first file is :

2006-09-16 09:00:01      1                0               501
2006-09-16 08:15:00      0                4               401
2006-09-15 07:05:15      0                3               301

The string is always that length, but the problem comes with the next two ints, because they can be of either length 1 or 2.

When I use my other file then I get :

2006-09-29 00:01:01     36               19               600
2006-09-30 21:00:00     9               33               599
2006-09-28 05:16:00     15               6               599

The second two lines are shifted to the left by 1 space. Is it possible to make it so that regardless of the length of the ints, they always appear in the same place removing the shift factor that occurs?

A: 

Tried this code inside Eclipse:

public class StringFormatting
{
    public static void main(String[] args) 
    {         
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-30 21:00:00", 9, 33, 599));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 15, 6, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
    }
}

I got the correct aligned formatted output:

2006-09-29 00:01:01     36               19               600

2006-09-30 21:00:00      9               33               599

2006-09-29 00:01:01     15                6               600

2006-09-29 00:01:01     36               19               600

Maybe it's Eclipse Console Window that is doing the right job here. Beats me...

Leniel Macaferi