
Grant E. answered 09/29/21
Second Year Computer Science Student
/*
I am coding this in Java and using the java.util.Scanner class (use import keyword in an actual file).
You could also use args[0] and args[1] for the 2 inputs, but that usually is only for console applications.
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter width of each side of box: ");
try {
int width = s.nextInt();
if (width < 5 || width > 21) {
throw new IllegalArgumentException("Invalid width!");
}
System.out.println("Enter character that boxes are made of: ");
String inputString = s.next();
if (s.length() != 1) {
throw new IllegalArgumentException("Invalid character!");
}
draw(s.charAt(0), width);
} catch (Exception e) {
System.out.println("Invalid input!");
}
s.close();
}
public void draw(char c, int width) {
for (int y = 0; y < width; y++) {
for (int x = 0; x < width; x++) {
if (x == 0 || y == 0 || x == width - 1 || y == width - 1) { //conditions for edges of box
System.out.print(c);
}
else {
System.out.print(" "); //print blank space if not on an edge
}
} //x for loop
System.out.println(); //go to next line (next y value)
} //y for loop
}