Hi,

I am facing problem with my coding but can't seems to find the problem with it. Can someone guide me?

//for loop
			for (line=1; line <=height; line++)
			{	for(col=1; col <=line; col++)
				{
					if (line%2 !=0)// odd line
					{
						if (col%2 != 0)//odd line,odd col
						System.out.print(A);
					
						else //(col%2 == 0) //odd line, even col
						System.out.print(B);
		
					}
					
					if (line%2 == 0)//even line
					{
						if(col%2 !=0)//even line, odd col
						System.out.print(A);
						
						else //(col%2 == 0)//even line, even col
						System.out.print(B);
					}
				}
			}
		}
		else 
			System.out.println (height + "- Error input!!");
	}

the output should be
AA
BBAA
AABBAA
when height = 3

but instead, my output is AAAABBAABBAA.

Thanks.

Regards,
Heels

Dani AI

Generated

Short diagnosis and fix (builds on 's hint). The immediate problem is newline placement: you need one newline per output row, so call println() after the inner loop, not inside it. Also, the pattern shown requires each row to print row groups of two identical characters (pairs), and the starting pair alternates by row parity — so the inner loop must either iterate groups and print each character twice, or iterate columns up to row*2. The original parity checks were more complicated than necessary.

A concise, working approach — iterate rows, iterate groups per row, pick the character by parity, print the chosen character twice, then print a newline after the groups:

for (int row = 1; row <= height; row++) {
    for (int grp = 1; grp <= row; grp++) {
        char ch = (row % 2 == grp % 2) ? 'A' : 'B';
        System.out.print(ch);
        System.out.print(ch); // print the pair
    }
    System.out.println(); // end of this row
}

Why this works: outer loop produces the needed number of rows; inner loop produces exactly row groups; the parity test chooses whether a group is 'A' or 'B' (it alternates each group and flips starting char each row); println() after the inner loop moves to the next output line.

Quick troubleshooting tips: 1) If you see too many lines, you put println() inside the inner loop; move it one level out. 2) If output lengths look doubled, check whether your A/B variables are strings longer than a single character. 3) To debug, print row and grp values temporarily so you can trace how many times each loop runs.

Recommended Answers

All 4 Replies

//for loop
            for (line=1; line <=height; line++)
            {   for(col=1; col <=line; col++)
                {
                    if (line%2 !=0)// odd line
                    {
                        if (col%2 != 0)//odd line,odd col
                        System.out.print(A);

                        else //(col%2 == 0) //odd line, even col
                        System.out.print(B);

                    }

                    if (line%2 == 0)//even line
                    {
                        if(col%2 !=0)//even line, odd col
                        System.out.print(A);

                        else //(col%2 == 0)//even line, even col
                        System.out.print(B);
                    }
                }
            }
        }
        else 
            System.out.println (height + "- Error input!!");
    }

There's one very obvious problem. You have no println statement (except for the error message) so everything is on one line. Stick one in the loop so you get new lines and see what the pattern is.

Sorry, i've put the println in the innest for loop, but it gives me 6 rows of results instead.

Enter height: 3
AA
BB
AA
AA
BB
AA
AA
BB
AA

I'm getting very very confuse now.

Sorry, i've put the println in the innest for loop, but it gives me 6 rows of results instead.

Enter height: 3
AA
BB
AA
AA
BB
AA
AA
BB
AA

I'm getting very very confuse now.

Well, if putting it in the inner loop gives you too many lines, try putting it in the outer loop instead. Step back and look. How many lines do you want? How many times does it go through the inner loop? How many times does it go through the outer loop? That tells you where to put the println.

Well, if putting it in the inner loop gives you too many lines, try putting it in the outer loop instead. Step back and look. How many lines do you want? How many times does it go through the inner loop? How many times does it go through the outer loop? That tells you where to put the println.

Thanks man! i finally can see it and understand this looping better now. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.