Home > Blockchain >  Align a String to the right using printf in Java
Align a String to the right using printf in Java

Time:10-01

I need to right justify the next output using Java:

class MyTree {

    public static void staircase(int n) {
        String myGraph = "#";
        for(int i=0; i<n; i  ){
            for(int x = 0; x <=i; x  ){
                if(x == i){
                    System.out.printf("%s\n", myGraph);
                }else{
                    System.out.printf("%s", myGraph);                    
                }
            }
        }
    }
}

I'm using printf function but I'm stuck, I tried different parameters but nothing seems to work; in case you have a suggestion please let me know.

Thanks

CodePudding user response:

You can't right justify unless you know the 'length' of a line, and there's no way to know that. Not all things System.out can be connected to have such a concept.

If you know beforehand, you can use

  • Related