Need help figuring out Java ASCII art pattern

I need to write a program to output an ASCII art pattern. The size of the pattern should change dynamically based on class constants.

It should look like this:

Number of boxes: 4
Width of boxes: 6
Height of boxes: 3
+------+------+------+------+
|      |      |      |      |
|      |      |      |      |
|      |      |      |      |
+------+------+------+------+

public class testing {
    public static void main(String[] args) {
        for (int height = 1; height <= 2; height++) {
            System.out.println("");
            for (int box = 1; box <= 4; box++) {
                System.out.print("+");
                // System.out.print("|");

                for (int width = 1; width <= 6; width++) {
                    System.out.print("_");
                }
            }
        }
    }
}

Answer

You are going to need to check if your loop is on the “edge” of a box and add different characters accordingly.

public class testing {
  public static void main(String args[]) {
     int height = 3;
     int width = 6;
     int numberOfBoxes = 4;
     String output = "";
     // height + 2 for the extra row of characters on the top and bottom
     for(int h = 0; h < height + 2; h++ ){
          for(int box = 0; box < numberOfBoxes; box++ ){
              // If on the outer edge, when h = 0 or h = height + 1
              if (h % (height + 1) == 0) {
                  output += "+";
                  for (int w = 1; w <= width; w++){ 
                          output += "-";
                      }
              // Otherwise only draw the vertical lines.
              } else {
                  output += "|";
                  for (int w = 1; w <= width; w++){ 
                          output += " ";
                      }
              }
        }
        // Add the last line of characters
        if (h % (height + 1) == 0) {
            output += "+";
        } else {
            output += "|";
        }
        // Add new line character
        output += "n";
     }
     System.out.println(output);
  }
}

However, above I added a lot of smaller string to the end of an output. Instead it would make more sense to use StringBuilder(). Adding Strings together is pretty inefficient and creates a lot of objects to only be used once and tossed. Instead (using StringBuilder()):

public class test {
  public static void main(String args[]) {
   int height = 3;
   int width = 6;
   int numberOfBoxes = 4;
   StringBuilder output = new StringBuilder();
   // height + 2 for the extra row of characters on the top and bottom
   for(int h = 0; h < height + 2; h++ ){
        for(int box = 0; box < numberOfBoxes; box++ ){
            // If on the outer edge, when h = 0 or h = height + 1
            if (h % (height + 1) == 0) {
                output.append("+");
                for (int w = 1; w <= width; w++){ 
                        output.append("-");
                    }
            // Otherwise only draw the vertical lines.
            } else {
                output.append("|");
                for (int w = 1; w <= width; w++){ 
                        output.append(" ");
                    }
            }
        }
        // Add the last line of characters
        if (h % (height + 1) == 0) {
            output.append("+");
        } else {
            output.append("|");
        }
        // Add new line character
        output.append("n");
   }
   System.out.println(output.toString());
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *