Pattern printing with Name Found this problem somewhere online and wanted to try this out with my beginner skills. I was able to solve this problem but how can I make my code better?

We need to print a pattern like this (each line has one more character in length):


E  
my  
iyt  
hTri  
bbiann  
i



Here is the solution I have written, and I want help in the following areas:

What am I doing wrong and how can I make it better?
How can I learn to write better code and logic?

public class nameIncrementing {

    public static void main(String[] args) {




                char[] text={'E','m','y','i','y','t','h','T','r','i','b','b','i','a','n','n','i'};
                int count =0;
                int length = text.length;
                int icnt=0;
                for(int i=0;i&lt;length;i++){
                    if(i==0)
                        System.out.println(text[0]);
                    else{
                        if(count+1&gt;=length)
                        {break;}
                        else{
                            for(int j=count+1;j&lt;i+2+count;j++){
                                icnt++;
                                if(j&gt;=length)
                                    break;
                                else
                                    System.out.print(text[j]);                              
                            }
                    count = count+icnt;
                    icnt=0;
                    }

                    System.out.println();}
                }
    }
}
First step - make your current code better

Instead of a char array you might prefer to use a String.
This would make the declaration easier:

String text = "EmyiythTribbianni";


I would say that most of us generally recommend that you shouldn't omit brackets.
You should also follow a consistent bracket pattern.

I recommend for every statment to be in a different line from your brackets.
This means that things like {break;} are not really that good.

Closing brackets should also be matching the same identation of the statment they were open.
By this I mean

ìf(condition){
    //code goes here
    for(declaration; evaluation; increment){
        //code goes here 
    }//closing bracket with the same indentation as the previous for statmeent
    //code goes here
}//closing bracket with the same indentation as the previous if statement


You should also follow a consistent spacing and let prefer more space than less.


This int count =0; becomes int count = 0;
This for(int i=0;i&lt;length;i++) becomes for(int i = 0; i &lt; length; i++)
This if(j&gt;=length) becomes if(j &gt;= length)


I think you get the ideia. Every time you find a comparison, assignment, +, -, or ; (on for) you space it.

Your code would then become something like this

String text = "EmyiythTribbianni";
int count = 0;
int length = text.length;
int icnt = 0;
for(int i = 0; i &lt; length; i++){
    if(i == 0){
        System.out.println(text[0]);
    }else{
        if(count + 1 &gt;= length){
            break;
        }else{
            for(int j = count + 1; j &lt; i + 2 + count; j++){
                icnt++;
                if(j &gt;= length){
                    break;
                }else{
                    System.out.print(text[j]);    
                }                   
            }
            count = count + icnt;
            icnt = 0;
        }
        System.out.println();
    }
}


Second step - taking a different approach

It seems that you want to put according to the number of character that you had printed so far.
I reckon that the arithmetic progression sum formula n(a1 + an) / 2 would be useful here.

The first time you print 1 character making the first term a1 = 1. Then you print a space.
The second time you print 2 characters a2 = 2, the total of characters print to this time is 2 * (1 + 2) / 2 = 3.
And so on.

By applying this idea you would get something along this lines:

String text = "EmyiythTribbianni";
int n = 1;

for(int i = 0; i &lt; length; i++){
    int sum = n  * (1 + n) / 2;
    if(i != sum){
        System.out.print(text[i]);
    }else{
        System.out.print(" ");
        System.out.print(text[i]);
        ++n;
    } 
}