Is there a way for System.out.printf() to take in a variable for setting the field width?

Refresh

March 2019

Views

203 time

1

I'm trying to display a pyramid to the console like so:

          1
        2 1
      3 2 1
    4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1

So far, my thoughts have been on printing a preset number of spaces before each row.

The formula I came up with is quite simple: numberOfSpaces = (totalRows - currentRow) * 2. For example, there would be 10 spaces for the first column. These 10 spaces are between the '6' at the bottom of the pyramid and the '1' at the top of the pyramid. For every row increment, the number of spaces decreases by 2.

Herein lies the problem. I can't figure out the correct syntax (or the existence of it) for the System.out.printf() method to take in a variable numberOfSpaces in setting the field width. This field width, in a rightmost aligned context, will allow me to print the spaces in each row.

Here is the code I have written so far. Although there are no compilation errors, I know there are likely many errors in the logic.

    public class DisplayPattern {
    public static void main (String[] args) {

        // Displays Pattern A.
        int integerPosition = 1;
        for (int x = 1; x <= 6; x++) {
            for (int y = 1; y <= integerPosition; y++) 
                System.out.print(y + " ");
            integerPosition++;
            System.out.println();
        }
        System.out.println();

        // Displays Pattern B.
        integerPosition = 6;
        for (int x = 1; x <= 6; x++) {
            for (int y = 1; y <= integerPosition; y++)
                System.out.print(y + " ");
            integerPosition--;
            System.out.println();
        }
        System.out.println();

        // Displays Pattern C.
        int totalRows = 6;
        int currentRow = 1;

        for (int x = 1; x <= 6; x++) {
            int numberOfSpaces = (totalRows - currentRow) * 2;
            for (int y = 1; y <= integerPosition; y++)
                System.out.printf("%(int)(numberOfSpaces)d", y);
            integerPosition++;
            currentRow++;
            System.out.println();
        }
        System.out.println();
    }
}

Of greatest concern is Part C. Part A and B, both involving no spaces, pose no problem.

http://i.imgur.com/wK4csvv.gif

Apologies on not being able to directly host an image here; I've apparently not enough reputation as a new user.

Thanks for helping a beginning and hobbyist programmer out!

2 answers

1

You're putting the name of your variable in the format string. You just need to build the string with your variable's value like this:

System.out.printf("%" + numberOfSpaces + "d", y);

Strictly speaking, the number in the format (between % and d) represents the whole number representation, so it includes the spaces AND the number of digits of your number y, so you'll have to calculate it and add it:

System.out.printf("%" + (numberOfSpaces + nbOfDigitsOfY) + "d", y);

Side note: if what you want is alignment, I think it's easier for you to calculate the final result than the number of spaces and the number of digits separately.

1

Here is another option

//n = the sequence 
int n = 3;
//s = the printed character
char s = '-';

System.out.println(String.format("%0" + n + "d", 0).replace('0',s))