Question

Building a rectangular with character input and specified column and rows. whitespace in the middle.

using standard input for string s, int r, int c

private static void printstuff(String s, int r, int c) {

    colums(s, c);
    rows(s, c, r);
    colums(s, c);

}

// straight columns 

private static void colums(String cs, int cc) {
    for (int i = 1; i <= cc; i++) {
        System.out.print(cs);
    }

}

this creates desired whitespace or "" to concat string with ie making x""""""""x

private static String whitespace(int wc) {
    String ws = " ";
    for (int i = 1; i <= wc - 3; i++) {
        ws += " ";
    }
    return ws;
}

whitespace to built a rectangular.

// downwards building

private static void rows(String rs, int rc, int rr) {
    String ws = whitespace(rc);
    for (int i = 1; i <= rr - 1; i++) {
        System.out.println(rs + ws + rs);

        // put strings together

    }

}

} whitespace and character rows to built a rectangular. needless to say it failed.

sample output:

XXXX    X  
X  X  
xxxx  

desired output:

xxxx  
x  x  
xxxx    
Was it helpful?

Solution

one quick solution below.. Cheers

public class Main {

public static void main(String[] args) {
    String s = "X";
    int totalColumns = 4;
    int totalRow = 3;

    colums(s, totalColumns);
    rows(s, totalColumns, totalRow);
    colums(s, totalColumns);


}

private static void colums(String cs, int cc) {
    for (int i = 0; i < cc; i++) {
        System.out.print(cs);
    }

}

private static String whitespace(int tc) {
    String ws = " ";
    for (int i = 1; i < tc - 2; i++) {
        ws += " ";
    }
    return ws;
}

private static void rows(String rs, int tc, int tr) {
    System.out.println();
    for (int i = 0; i < tr - 2  ; i++) {
        System.out.println(rs + whitespace(tc) + rs);
    }
}

}

OTHER TIPS

Im not sure if this what you want but throw a System.out.println(""); after the for loop in colums

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top