title: Aligning the output of a 2 dimensional array using printf?
body: <p>I have three separate arrays, one String for the header line, and two 2-dimensional arrays. One only has values along the left side for Row Titles. The other is double and has randomly-generated values. So far an example output looks like this:</p>
  
  <pre><code>Test 1     Test 2     Test 3     Test Average   
  Student1 65.0 61.0 64.0 63.333333333333336 
  Student2 63.0 64.0 80.0 69.0 
  Student3 60.0 100.0 89.0 83.0 
  Student4 80.0 62.0 77.0 73.0 
  Student5 68.0 75.0 68.0 70.33333333333333 
  Averages 67.2 72.4 75.6 71.73333333333333 
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.util.Random;
  public class averages { //This part randomly generates scores to use for the students
  public static int RNG1(int a) { 
      int min=60;//The minimum score a student can get is 60, for realism purposes
      int max=100;//Te max score is 100
      Random rand = new Random();
      int randomNum = rand.nextInt((max - min) + 1) + min;//Formula for a random number
          return randomNum;//Return result
      }   
  public static void main(String[] args)
  {
      String [] Header= { "    ", "Test 1", "Test 2", "Test 3", "Test Average" };//Header/column titles
      String [][] RowTitles= {//The Row titles
                {"Student1", "", "", "", ""},
                {"Student2", "", "", "", ""},
                {"Student3", "", "", "", ""},
                {"Student4", "", "", "", ""},
                {"Student5", "", "", "", ""},
                {"Averages", "", "", "", ""}
              };
      int a=0;        
      //The next bunch of lines are variables defined by random numbers
      //That will be inserted into the table as test scores
      //
          double oneone=RNG1(a);
          double onetwo=RNG1(a);
          double onethree=RNG1(a);
          double onefour=RNG1(a);
          double onefive=RNG1(a);
          double twoone=RNG1(a);
          double twotwo=RNG1(a);
          double twothree=RNG1(a);
          double twofour=RNG1(a);
          double twofive=RNG1(a);
          double threeone=RNG1(a);
          double threetwo=RNG1(a);
          double threethree=RNG1(a);
          double threefour=RNG1(a);
          double threefive=RNG1(a);
  
          //The following lines are averages of the individuals and of the tests
          //
          double stuone= (oneone+twoone+threeone)/3;
          double stutwo= (onetwo+twotwo+threetwo)/3;
          double stuthree= (onethree+twothree+threethree)/3;
          double stufour= (onefour+twofour+threefour)/3;
          double stufive= (onefive+twofive+threefive)/3;
  
          double testone= (oneone+onetwo+onethree+onefour+onefive)/5;
          double testtwo= (twoone+twotwo+twothree+twofour+twofive)/5;
          double testthree= (threeone+threetwo+threethree+threefour+threefive)/5;
          double testfour= (stuone+stutwo+stuthree+stufour+stufive)/5;        
          //array for test scores and averages
          double [][] TestScores=
              {   
                  {0, oneone, twoone, threeone, stuone},
                  {0, onetwo, twotwo, threetwo, stutwo},
                  {0, onethree, twothree, threethree, stuthree},
                  {0, onefour, twofour, threefour, stufour},
                  {0, onefive, twofive, threefive, stufive},
                  {0, testone, testtwo, testthree, testfour}
              };         
          //Prints out header:
          for (int  r = 0;  r &lt; Header.length;  r++)
          {
              if(r &lt; Header.length-1)
              {
                  System.out.print("  "+Header[r]+"   ");
          } else {
                  System.out.println("  "+Header[r]+"   ");
              }                   
          }       
          //Prints out student names and test scores
          for (int  r = 0;  r &lt;= 5;  r++)
          {   
              for (int  c = 0;  c &lt; 4;  c++)
              {              
                  if(c==0){System.out.print(RowTitles[r][c]+" ");
              }                  
              if(c&gt;0){
                  System.out.print(TestScores[r][c]+" ");
              }                  
              if(c==3){
                  System.out.println(TestScores[r][4]+" ");
              }
          }
      }
  }
  </code></pre>
  
  <p>For the life of me, I can't figure out how to use <code>printf</code>. I assume it would be used in the last non-bracket line, instead of <code>System.out.println(TestScores[r][4]+" ");</code> but I can't get it to work. Thanks.</p>