Simple Java Pyramid - Using System.out.printf() to format output

TheNoviceCoder :

The Goal:

I'm attempting to produce a pyramid similar to the format given below. This requires a basic Java program that accepts user input, converts from numbers to strings, uses nested loops, and generates formatted output. Here is an example of the desired output using 8 rows.

Enter the number of lines: 8

               1              
             2 1 2            
           3 2 1 2 3          
         4 3 2 1 2 3 4        
       5 4 3 2 1 2 3 4 5      
     6 5 4 3 2 1 2 3 4 5 6
   7 6 5 4 3 2 1 2 3 4 5 6 7
 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8

The Problem:

I believe I have the logic to properly increment the numbers, however I need help with formatting the pyramid. I am able to add spaces between each number, but if the number of lines is > 10, then the formatting is messed up as you can see. On the final line (line 10), the number 1 is no longer centered. What is the reason and how can I solve this?

I know I can use System.out.printf("%4s", value), but want to find a way to do this without hard-coding in case the number of rows is > 1000. Thank you in advance for any guidance your much more knowledgeable minds can give me.

                   1
                 2 1 2
               3 2 1 2 3
             4 3 2 1 2 3 4
           5 4 3 2 1 2 3 4 5
         6 5 4 3 2 1 2 3 4 5 6
       7 6 5 4 3 2 1 2 3 4 5 6 7
     8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
   9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10

My Current Code:

import java.util.Scanner;

public class Pyramid1
{
    public static void main(String[] args)
    {
        int i, j, k, a;

        //Create a Scanner object
        Scanner input = new Scanner (System.in);

        //Prompt the user to enter number of rows in pyramid
        System.out.print("Enter number of rows: ");

        int rows = input.nextInt();
        a = rows;

        //Logic
        for (i=1; i<=rows; i++)
        {
            for (j=a; j>1; j--)
            {
                System.out.printf(" %s", " ");
            }

            for (k=i; k!=0; k--)
            {
                String str1 = "" + k;
                System.out.printf(" %s", str1);
            }
            a--;

            for (int l=2; l<=i; l++)
            {
                String str2 = "" + l;
                System.out.printf(" %s", str2);
            }

            System.out.println();
        }
    }
}
Gnanasekaran Palanisamy :

I here is code for your requirement

import java.util.Scanner;

public class Pyramid1
{   
    public static void main(String[] args)
    {
        int i, j, k, a;

        //Create a Scanner object
        Scanner input = new Scanner (System.in);

        //Prompt the user to enter number of rows in pyramid
        System.out.print("Enter number of rows: ");

        int rows = input.nextInt();
        a = rows;

        int length = ("" + rows).length();

        String str = " %"+length+"s";

        for (i=1; i<=rows; i++)
        {
            for (j=a; j>1; j--)
            {
                System.out.printf(str, " ");
            }

            for (k=i; k!=0; k--)
            {
                String str1 = "" + k;
                System.out.printf(str, str1);
            }
            a--;

            for (int l=2; l<=i; l++)
            {
                String str2 = "" + l;
                System.out.printf(str, str2);
            }

            System.out.println();
        }
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463276&siteId=1