# Thread: Printing a rectangle with a hollow centre problem

1. Member
Join Date
Jun 2014
Posts
3
Rep Power
0

## Printing a rectangle with a hollow centre problem

Hi guys
I'm both new to Java and this forum, so apologies if I do anything wrong here

I'm working through the Java Just in Time textbook by John Latham, by recommendation
So far, it's been a very good guide and the set problems quite fun to solve
However, I'm stuck on this problem: 6.4.2, page 28 http://studentnet.cs.manchester.ac.u...-with-labs.pdf

So here's my code as it is right now:
Java Code:
```public class PrintHoledRectangle
{
public static void main(String[] args)
{
int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
int height = Integer.parseInt(args[1]) / 2 * 2 + 1;
int centre = width * height / 2 + 1;
int index = 0;

for (int row = 1; row <= height; row = row++)
{
for (int column = 1; column <= width; column = column++)
{
index = index++;
if (index != centre)
System.out.print("[_]");
else
System.out.print("   ");
}

System.out.println();
}
}
}```
The width of the rectangle to be printed is given by the first argument, the height by the second
The centre tile ("[_]") of the rectangle (to be printed as " ") is given by the formula: ((width*height)/2)+1, assuming you count each tile left-to-right, top-to-bottom
My thinking is that the programme keeps a running count (seen as 'index') so that, when the centre tile is to be printed, it outputs " ", otherwise "[_]", hence the if-else statement

Currently, it prints "[_]" to infinity

Could anyone please point me in the right direction? Thanks in advance!

2. ## Re: Printing a rectangle with a hollow centre problem

What does the code print when it is executed? Wrap the output in code tags to preserve the interior spaces.

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Printing a rectangle with a hollow centre problem

He, this is a good one. Lets start with a little debugging tip to help you narrow down the source of problems yourself. Try printing out row and column in the loops and see what you get. The reason why it loops infinitely should then become clear, and then there will be second puzzle to solve.

Java Code:
```   public static void main(String[] args)
{
int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
int height = Integer.parseInt(args[1]) / 2 * 2 + 1;

int centre = width * height / 2 + 1;
int index = 0;

for (int row = 1; row <= height; row = row++)
{
for (int column = 1; column <= width; column = column++)
{
System.out.println("R: " + row + ", C:" + column);
}
}
}```

4. Member
Join Date
Jun 2014
Posts
3
Rep Power
0

## Re: Printing a rectangle with a hollow centre problem

Originally Posted by Norm
What does the code print when it is executed? Wrap the output in code tags to preserve the interior spaces.
Originally Posted by gimbal2
He, this is a good one. Lets start with a little debugging tip to help you narrow down the source of problems yourself. Try printing out row and column in the loops and see what you get. The reason why it loops infinitely should then become clear, and then there will be second puzzle to solve.

Java Code:
```   public static void main(String[] args)
{
int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
int height = Integer.parseInt(args[1]) / 2 * 2 + 1;

int centre = width * height / 2 + 1;
int index = 0;

for (int row = 1; row <= height; row = row++)
{
for (int column = 1; column <= width; column = column++)
{
System.out.println("R: " + row + ", C:" + column);
}
}
}```
Wahay, got it!
Seems I didn't understand how to use the "x++" operator

Thanks muchly for the help, one last thing:

I'm not sure I fully understand how my misuse of the operator messed up the programme so
"x=x++" translates to "x=x=x+1", right? So is it that java causes that to be read as "x=x", due to the dominance of a term closer to the start of the line, and disregards the "=x+1" bit?

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Printing a rectangle with a hollow centre problem

Good job figuring it out. The 'problem' here is that you are using the 'post-increment operator' which requires you to know -exactly- how that thing behaves - as you have already found out, it works in mysterious ways that often lead to wrong assumptions. The code isn't really translated to what you say, its more that the ++ operation you are performing is simply 'lost in translation'. There are some good reads to be found about it on the net about that if you google 'java post increment operator'. Stackoverflow is a good source of answers:

post increment operator java - Stack Overflow

6. Member
Join Date
Jun 2014
Posts
3
Rep Power
0

## Re: Printing a rectangle with a hollow centre problem

Sweet! Thanks very much for all the help :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•