1

I am making a basic spreadsheet program, and by default I have the cell width set to 12 spaces. I am using printf to do this.

System.out.printf("%12s|", rowsAndColumns[i][j]);

How can I do something like.

System.out.printf("%cellWidths|", rowsAndColumns[i][j]);

Is this even possible?

Thanks.

  • 1
    See answers to [How can I pad a String in Java?](http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java). – PM 77-1 Dec 26 '13 at 22:18
  • @Nick are you creating a spreadsheet that writes to the standard output? I think you ought to be using a GUI for that, isn't it? – prmottajr Dec 26 '13 at 22:19
  • The spreadsheet isn't using a GUI, its using print statements to print the rows and columns. –  Dec 26 '13 at 22:21

1 Answers1

3

I don't think what you are looking for is possible.

But why don't you do it as follows -

System.out.printf("%" + cellWidths + "|", rowsAndColumns[i][j]);
Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139