   import java.util.Scanner;
   import java.util.InputMismatchException;
   
	/**
	 *  Takes user input and generates an octagon of given size.
	 *
	 *  @author [Student Name]
	 *  @date Jan. 10, 2013
	 */

   public class Program2 {
      public static void main (String args []) {
         int userInput = 0;
      
      //Asks for user input for size of octagon.
         System.out.println("This program draws an octagon.");
         Scanner keybd = new Scanner(System.in);
         boolean test = true;
         boolean keepGoing = true;;
      	
         while (keepGoing) {
            while (test) {
               try {
                  test = false;
                  System.out.print("Please enter a size, as an integer (1-12): ");
                  userInput = keybd.nextInt();
                  if (userInput < 0 || userInput > 12) {
                     System.out.println("Sorry. Please enter an integer from 1-12, inclusively.");
                     test = true;
                  }
                  else if (userInput == 0) {
                     keepGoing = false;
                  }  	
               }
                  catch (InputMismatchException exception) {
                     System.out.println("Sorry. Please enter an integer from 1-12, inclusively.");
                     String input = keybd.nextLine();
                     test = true;
                  }
            }  
            int spaceCount = userInput;
            int hashCount = userInput;
            String hash = "#";
            String space = " ";
         
         //Creates top third of octagon.
            int topCount = userInput;
            while (topCount > 0) {
               int lineSpaceCount = spaceCount;
               int lineHashCount = hashCount;
            
               while (lineSpaceCount > 0) {
                  System.out.print(space);
                  lineSpaceCount--;
               }
               while (lineHashCount > 0) {
                  System.out.print(hash);
                  lineHashCount--;
               }
               while (lineSpaceCount < spaceCount) {
                  System.out.print(space);
                  lineSpaceCount++;
               }
               System.out.println();
               spaceCount--;
               hashCount += 2;
               topCount--;
            }
         
         //Creates middle third of octagon.
            hashCount = userInput;
            int middleCount = userInput;
            while (middleCount > 0) {
               hashCount = userInput * 3;
               while (hashCount > 0) {
                  System.out.print(hash);
                  hashCount--;
               }
               System.out.println();
               middleCount--;
            }
         
         //Creates bottom third of octagon.
            hashCount = (userInput * 3) - 2;
            spaceCount = 1;
            int bottomCount = userInput;
         
            while (bottomCount > 0) {
               int lineHashCount = hashCount;
               int lineSpaceCount = spaceCount;
               while (lineSpaceCount > 0) {
                  System.out.print(space);
                  lineSpaceCount--;
               }
               while (lineHashCount > 0) {
                  System.out.print(hash);
                  lineHashCount--;
               }
               System.out.println();
               bottomCount--;
               spaceCount++;
               hashCount -= 2;
            }
            keepGoing = false;
         }
      }
   }
 