Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Using printf with a ternary operator

I am trying to print a pyramid of numbers that looks like this:

                          1
                        1 2 1 
                      1 2 3 2 1  

etc. to a maximum of 15

The spacing needs to be consistently 3 spaces even when the numbers are two digits in length. I have a ternary solution for the basic problem but the spacing becomes uneven starting when x = 10. Is there a way to use printf with this program (code follows below) or do I need to abandon the ternary operator and go back to if/else in order to make it work. I like the streamlined version with the ternary operator.

Thanks in advance

public class numberPyramid {

    public static void main(String [] args){

        Scanner in = new Scanner(System.in);
        int x = in.nextInt();                   
         for (int i=1; i<=x; i++){
       for (int j=1; j<=x-i; j++) {
               System.out.print("  ");
           }

        for (int m=1; m<=i; m++) {
              System.out.print((m>=15)?+m: " " + m );
        }

       for(int m=i-1; m>=1; m--)  {
             System.out.print((m>=15)?+m: " " + m );

           }
       System.out.println("  ");                                      
       }
    }
}

2 Answers

If I read you correctly, you seem to have conflicting goals. You say you want a consistent 3 spaces between entries, whether they are 1 or 2 digits long. But that's not possible if you also want the numbers to line up vertically.

To help see what's going on I modified your code to show an underscore for each of the initial spaces that are printed by your first inner for loop. If you want to go to 12 rows you will need to print 3 spaces, rather than 2 (so my revised version below prints 3 underscores ___ each time through the loop instead of two). Next, in the second inner loop, you need to print two spaces before a number if the number is 1 digit long, and one space if it is 2 digits long. To show this I replace your spaces with carets (^). Notice how there are two before single digits, but only one before the two digit numbers. Then, in the third inner for loop, you need again to print two spaces after single digits and 10 and one after double digits other than 10. To show this I've replaced your spaces with hyphens. Finally, you can use just println() instead of println(" ") to force a new row at the end of each outer loop. I also added a prompt for Scanner.

How many rows? 12
_________________________________^^1
______________________________^^1^^2--1
___________________________^^1^^2^^3--2--1
________________________^^1^^2^^3^^4--3--2--1
_____________________^^1^^2^^3^^4^^5--4--3--2--1
__________________^^1^^2^^3^^4^^5^^6--5--4--3--2--1
_______________^^1^^2^^3^^4^^5^^6^^7--6--5--4--3--2--1
____________^^1^^2^^3^^4^^5^^6^^7^^8--7--6--5--4--3--2--1
_________^^1^^2^^3^^4^^5^^6^^7^^8^^9--8--7--6--5--4--3--2--1
______^^1^^2^^3^^4^^5^^6^^7^^8^^9^10--9--8--7--6--5--4--3--2--1
___^^1^^2^^3^^4^^5^^6^^7^^8^^9^10^11-10--9--8--7--6--5--4--3--2--1
^^1^^2^^3^^4^^5^^6^^7^^8^^9^10^11^12-11-10--9--8--7--6--5--4--3--2--1

Here's the revised version of your code that will produce the above output.

import java.util.Scanner;

public class numberPyramid {
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        System.out.print("How many rows? ");  //prompt the user for input
        int x = in.nextInt();

        for (int i=1; i<=x; i++){
           for (int j=1; j<=x-i; j++) {
              System.out.print("___");  //using 3 underscores instead of 3 spaces
           }

           for (int m=1; m<=i; m++) {
              //System.out.print((m>=11) ? "^" + m : "^^" + m );
              String s = "" + m;  //need to get number of digits
              int len = s.length();
              if (len == 1) {
                  System.out.print("^^" + m);  //using 2 ^ instead of 2 spaces
              } else {
                  System.out.print("^" + m);
              }
           }

           for(int m=i-1; m>=1; m--) {
              //System.out.print((m>=11) ? " " + m : "  " + m );
              String s = "" + m;
              int len = s.length();
              if (len == 1) {
                  System.out.print("--" + m);  //using 2 - instead of 2 spaces
              } else {
                  System.out.print("-" + m);
              }              
           }
           //System.out.println("  "); 
           System.out.println();                                     
        }

    }
}
Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

I figured out a way that worked without the ternary operator (making a String variable of the spaces and using printf for spaces.

The ternary operator was not a mandated part of the assignment. They look more concise and I am trying to understand the syntax better.

NJM