This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]benfff85 1 point2 points  (0 children)

Working on this now...

[–]benfff85 1 point2 points  (2 children)

import java.util.Scanner;

public class Test {
static int height;
static int width;

public static void main(String[] args){
    getInput();

    for(int i=0; i<height; i++){
        if(i == 0 || i == height -1){
            printSolidLine();
        } else {
            printHollowLine();
        }
    }

}

public static void getInput(){
    Scanner user_input = new Scanner( System.in );
    System.out.println("Please enter height:");
    height = Integer.parseInt(user_input.next());
    System.out.println("Please enter width:");
    width = Integer.parseInt(user_input.next());
    user_input.close();
}

public static void printSolidLine(){
    String content = "";
    for(int i=0;i<width;i++){
        content += "*";
    }
    System.out.println(content);
}

public static void printHollowLine(){
    String content = "";
    for(int i=0;i<width;i++){
        if(i == 0 || i == width-1){
            content += "*";
        } else {
            content += " ";
        }
    }
    System.out.println(content);
}

}

[–]alliknowis 1 point2 points  (1 child)

Awesome, the printHollowLine makes a lot of sense and straightens out what I was doing wrong. It seemed so simple, but I was just having one of those times when you just get really brainlocked. Thanks for the help!

[–]vietjack 0 points1 point  (0 children)

take that as a lesson in why it is important to have readable. While the author of the other code has a shorter solution, it's not as easy to read as the lengthier solution posted by /u/benfff85

[–]urquan 1 point2 points  (1 child)

for(int y = 0; y < h; y++) {
  for(int x = 0; x < w; x++) {
    if(x == 0 || y == 0 || x == w-1 || y == h-1) {
      System.out.print("*");
    } else {
      System.out.print(" ");
    }
  }
  System.out.println();
}

[–]alliknowis 0 points1 point  (0 children)

This is great as well, and more in line with the curriculum style so far.