0

I want to be organize with the output so I want to do something like this

   1   2   3   4   5
........
  91  92  93  94  95
........
 101 102 103 104 105
etc....

So I was trying to figure out how to line them up with the correct amount of spacing between each number.

my first attempt is doing everything manually like this

System.out.print("  " + counter);

using a loop

user3345335
  • 33
  • 1
  • 1
  • 6

1 Answers1

1

Read Formatting Numeric Print Output in the docs.

You can use System.out.format using %<width>d where you replace <width> with the width you require (make sure everything fits in!):

System.out.format("%4d", number); // align to width of 4

To add newlines use %n:

System.out.format("%4d%n", 1);
System.out.format("%4d%n", 12);
System.out.format("%4d%n", 123);
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88