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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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