DoodleCode
DoodleCode

Reputation: 11

Printing Column Tables in Java Using printf

I am trying to print column tables using printf in Java. How can I go about moving theses columns under each other?

Currently I am getting this:

But this is what I want:

Here is my code

import java.util.Scanner;

public class LabTimeTable{

   public static void main(String [] args){
   Scanner input = new Scanner(System.in);

   System.out.println("Time Table:");

   System.out.print("Number (1-10): ");
   int number1 = input.nextInt();
   int number2 = (number1+1);
   int number3 = (number2+1);
   int number4 = (number3+1);
   int number5 = (number4+1);
   int number6 = (number5+1);
   int number7 = (number6+1);
   int number8 = (number7+1);
   int number9 = (number8+1);
   int number10 = (number9+1);


      for(int i = 1; i < 11; i++){

         System.out.printf("%2d * %2d = %2d", i, number1, (number1*i));
         System.out.printf("%10d * %2d = %2d", i, number2, (number2*i));
         System.out.printf("%10d * %2d = %2d", i, number3, (number3*i));
         System.out.printf("%10d * %2d = %2d", i, number4, (number4*i));
         System.out.printf("%10d * %2d = %2d", i, number5, (number5*i));
         System.out.printf("%10d * %2d = %2d", i, number6, (number6*i));
         System.out.printf("%10d * %2d = %2d", i, number7, (number7*i));
         System.out.printf("%10d * %2d = %2d", i, number8, (number8*i));
         System.out.printf("%10d * %2d = %2d", i, number9, (number9*i));
         System.out.printf("%10d * %2d = %2d", i, number10, (number10*i));
         System.out.println();

      }


   }

Upvotes: 0

Views: 535

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

printf doesn't add a newline to the end of the output, for that you should probably use %n

Next, you need to leave a number of blank spaces between the two columns, this can easily be established by using %10s, where 10 is the number of characters you want the two columns separated from.

After that, you just need to decide if you want to do all in one statement...

System.out.printf("%2d * %2d = %2d%10s%2d * %2d = %2d%n", index, multiplier, (multiplier * index), "", index, multiplier + 1, ((multiplier + 1) * index));

or use multiple statements...

System.out.printf("%2d * %2d = %2d", index, multiplier, (multiplier * index));
System.out.printf("%10s%2d * %2d = %2d%n", "", index, multiplier + 1, ((multiplier + 1) * index));

Runnable example...

public class Test {

    public static void main(String[] args) {

        int multiplier = 1;
        for (int index = 1; index < 11; index++) {
            //System.out.printf("%2d * %2d = %2d%10s%2d * %2d = %2d%n", index, multiplier, (multiplier * index), "", index, multiplier + 1, ((multiplier + 1) * index));
            System.out.printf("%2d * %2d = %2d", index, multiplier, (multiplier * index));
            System.out.printf("%10s%2d * %2d = %2d%n", "", index, multiplier + 1, ((multiplier + 1) * index));
        }

    }
}

Upvotes: 1

Related Questions