# Thread: Help with printing hollow squares?

1. Member
Join Date
Oct 2012
Posts
2
Rep Power
0

## Help with printing hollow squares?

Hi guys,
My assignment is to write a program to take a user input from 0-64 for n, and print a square with sides of length n. The corners are to be plus signs (+) while the edges straight lines (- or |) like so

Enter length between 0 and 64 (-1 to exit): <4>
length entered: 4
+--+
| |
| |
+--+

Also, I need the program to loop continuously, asking the user for new inputs and printing the new square until the input is -1, at which point the program exits, totaling the number of squares printed as well as the average length the user entered. Lastly, I must use a while, do-while, and for statement at some point in the code. My problem is that after the first square is printed, the following inputs don't print a square properly. I would greatly appreciate some pointers as to where I went wrong. Here is the code so far. Thank you!

Java Code:
```import java.util.Scanner;

public class PrintSquares {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
int row =1;
int column;
int squares = 0;
double total = 0;

System.out.println("Enter length between 0 and 64 (-1 to exit): ");
n = input.nextInt();

if (n>64 || n<-1) {
System.out.println(n+" is invalid\nLength must be between 0 and 64 inclusive, or enter -1 to exit.");
System.exit(1);
}

else do	{
squares++;
total = total + n;
while ( row <= n )
{
for (column=1; column <= n; column++ )
{
if (row == 1 && column == n || row == n && column == 1 || row == n && column == n || row == 1 && column == 1)
System.out.print("+");
else if ( row == 1 )
System.out.print( "-" );
else if ( row == n )
System.out.print( "-" );
else if ( column == 1 )
System.out.print( "|" );
else if ( column == n )
System.out.print( "|" );
else
System.out.print( " " );
}
System.out.println();
row++;
}System.out.println("Enter length between 0 and 64 (-1 to exit): ");
n = input.nextInt();
if (n>64 || n<-1) {
System.out.println(n+" is invalid\nLength must be between 0 and 64 inclusive, or enter -1 to exit.");
System.exit(1);
}
}while (n != -1);

if (n==-1) {
System.out.println(squares+" squares printed. Average length: "+(total/squares));
System.exit(0);
}
}
}```

2. ## Re: Help with printing hollow squares?

The best advice I can give you is: use methods.

You could have a method that just prints the top/bottom line. (methodA)
have a method that prints a single line for the sides. (methodB)

Then you could have another method that:
calls methodA
calls methodB n-2 times
class methodA

Then the main method would loop asking user input as long as it isn't -1.

The advantage to this is that you have broken the code into smaller chunks and you can work on each chunk one at a time. If there is a problem then you know it exists within that small chunk than somewhere in ALL your code if you have it crammed together in the main method.

3. Member
Join Date
Oct 2012
Posts
2
Rep Power
0

## Re: Help with printing hollow squares?

Thanks so much for your reply. We haven't actually covered methods yet in this class, but I did a little bit of extra reading and was able to figure the rest out. I really appreciate 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
•