0

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();

      }


   }
0

1 Answer 1

1

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));
        }

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@HovercraftFullOfEels I don't disagree with closing the question, but I think I voted for "asking for debugging assistance..." instead
@HovercraftFullOfEels Agreeded - JTree is to complex to work on with assumptions alone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.