word word
word word

Reputation: 457

How to format an array with printf() in Java?

I know how to format single variables, but I do not know how to format an array with printf(). I want to display my program with each number lining up with each other vertically, with each number on each line with the same amount of spaces. Below is an example of what I mean with different numbers:

1.1   2.2   3.3   4.4   5.5
1     2     3     4     5

Here is the code that I have for trying to display the numbers:

 // create array
 String[] tempsArray2 = temps2.toArray(new String[0]);

 // for-each loop
 for(String ss : tempsArray2){

     // display ss to make sure program works correctly
     System.out.printf("%40s", ss);

When I run it, the program displays like this:

run:
70.3   70.8   73.8   77.0   80.7   83.4   84.5   84.4   83.4   80.2   76.3   72.0   
     69 67 66 64 66 69 67 67 70 69 69 70BUILD SUCCESSFUL (total time: 0 seconds)

How can I fix this so that I can format the whole array once with a single System.out.printf(); statement, or do I have to format them one by one? Any help will be greatly appreciated.

Upvotes: 3

Views: 40076

Answers (2)

Alex Vallejo
Alex Vallejo

Reputation: 1351

Assuming you are working with a 2D array filled with doubles, you can do something like this to get nicely formatted output:

double arr[][] = {
  {1.012,2.123,3.345,4.456,5.543,6.543,7.456,8.673,9.456,0.356},
  {1.133,2.135,3.456,4.654,5.234,6.067,7.456,8.432,9.235,0.959}
};

for (int row = 0; row < arr.length; row ++){
    for (int col = 0; col < arr[0].length; col++){
          System.out.printf("%-7.3f", arr[row][col]);
    }
    System.out.printf("\n");
}

The - aligns the numbers to the left and puts them all in nice columns. The 7 is the field width and tells printf how wide to make your columns. The .3 is the precision of the numbers (decimal places) that will print. Lastly, the f stands for float which is the format specifier for doubles. You can look up the syntax for java's printf in the docs.

Upvotes: 1

Jason C
Jason C

Reputation: 40335

If you want that specific control over formatting, you have to do it to each item individually. There is Arrays.toString(), but it provides no control over individual elements.

If you are printing multiple arrays and you want their output to "line up", you will need to choose output formats so that happens, e.g.:

    float data1[] = new float[] { 1, 2.5f, 30, 4.56f, 5 };
    int data2[] = new int[] { 1, 22, 33, -4, 5 };

    for (float f:data1) System.out.printf("%6.2f ", f);
    System.out.println();
    for (int n:data2) System.out.printf("%3d    ", n);
    System.out.println();

Which outputs:

  1.00   2.50  30.00   4.56   5.00 
  1     22     33     -4      5  

That example might not be the precise format you are going for, but the point is, you have control this way. If you want to do left-aligned numbers, wider fields, if your types are Strings or floats or doubles or ints, etc.

Sometimes it helps to work out field widths and padding spaces in e.g. a text editor or on paper first. E.g. with the above, where we try to line up decimal points and digits between floats and integers, I like to first work out the format in a fixed-width editor (like in a comment or in notepad), e.g.:

FFF.FF FFF.FF FFF.FF FFF.FF
III    III    III    III

Then you can more easily see how that translates to format specifiers:

  • FFF.FF  is "%6.2f ".
  • III     is "%3d    ".

See the documentation for Formatter for more information about the format codes you can pass to System.out.printf().

Upvotes: 4

Related Questions