user14363905
user14363905

Reputation:

Removing blank line from end of result in java

This is what my result looks like:

1.*********
2.*********
3.*********
4.*********
5.*********
6.*********
7.

I need to remove the blank line 7

Here is my code:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    new UnsupportedOperationException("You must implement this method.");
    if (maxRows<1 || maxCols<1) { 
        return null; 
    } else { 
        String result = "";
        for (int i = 0; i < maxRows; i++) { 
            for (int j = 0; j < maxCols; j++) { 
                result = result + symbol;
            } 
            result = result + "\n";
        } 
        return result;   
    }
}

Upvotes: 1

Views: 402

Answers (5)

Nowhere Man
Nowhere Man

Reputation: 19545

Another way to build a repeated rectangle without the trailing new line is to use combination of ancient Collections.nCopies and String.join (Java 8+):

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    }
    String row = String.join("", Collection.nCopies(maxCols, String.valueOf(symbol)));
    return String.join("\n", Collections.nCopies(maxRows, row));                                
}

Upvotes: 0

Nikolas
Nikolas

Reputation: 44398

Since the Java version wasn't specified, as of Java 11, you can use String::repeat with amended number of rows and columns:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    } else {
        String row = String.valueOf(symbol).repeat(maxCols);  // create a row
        String columns = (row + "\n").repeat(maxRows - 1);    // create n-1 columns 
        return columns + row;                                 // append the last column 
                                                              // .. without the new line
    }
}

If you are stick with a lower version of Java, use StringBuilder. The advantage of this class is they can be used with each other:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    } else {
        StringBuilder row = new StringBuilder();
        StringBuilder column = new StringBuilder();
        for (int i = 0; i < maxCols; i++) {        // rows
            row.append(symbol);
        }
        for (int i = 0; i < maxRows - 1; i++) {    // n-1 columns
            column.append(row).append("\n");
        }
        return column.append(row).toString();      // append the last one w/o a new line
    }
}

Upvotes: 1

Aziz Sonawalla
Aziz Sonawalla

Reputation: 2502

You don't need any extra conditions. Just replace return result with return result.trim()

Upvotes: 1

Chaitanya
Chaitanya

Reputation: 3638

Just adding a simple condition will help

    public static String getRectangle(int maxRows, int maxCols, char symbol) {
//        new UnsupportedOperationException("You must implement this method.");
        if (maxRows < 1 || maxCols < 1) {
            return null;
        } else {
            String result = "";
            for (int i = 0; i < maxRows; i++) {
                for (int j = 0; j < maxCols; j++) {
                    result = result + symbol;
                }
                if (i != maxRows - 1) result = result + "\n";
            }
            return result;
        }
    }

Upvotes: 0

Rafay
Rafay

Reputation: 92

Add a condition here:

if (i != maxRows - 1) {
    result = result + "\n";
}

Although i would suggest using a StringBuilder instead of a string to store the result.

Upvotes: 3

Related Questions