Help with printing hollow squares?

• 10-19-2012, 04:53 AM
AZjava_newbie
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!

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); } } }```
• 10-19-2012, 05:07 AM
Junky
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.
• 10-22-2012, 05:14 AM
AZjava_newbie
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!