Search code examples
javareturn-value

Is there a way to avoid the "null" return?


I'm working on a method which draws a rectangle using asterisks. My method does return the desired rectangle, yet it obliges me to add a return statement with a "null" value. How can I get rid of this "null" value? Using "void" instead of "String" in the method did not work.

My code is the following:

public class Rectangle {
   
    int width;
    int height;
    
    public Rectangle() {
        this.width = 0;
        this.height = 0; 
    }

    public String draw() {
        for(int i = 1; i <= this.height; i++){
            for(int j = 1; j <= this.width; j++){
                System.out.print("* ");
            }
            System.out.print("\n");   
        }      
        return null;
    }

    public String toString() {
        return draw();
    }
}

By running this code:

public class Essay {
    public static void main(String[]args) {
        Rectangle rectangle = new Rectangle(5, 3);

        System.out.println(rectangle.toString());
    }
}

the result is:

enter image description here


Solution

  • So, draw doesn't actually return anything (useful), instead you're just printing the stars directly to the output stream.

    Instead, what you should (probably be) doing, is building a String within the draw method which can be returned to the caller, for example...

    public String draw() {
        StringJoiner outter = new StringJoiner("\n");
        for (int i = 1; i <= this.height; i++) {
            StringJoiner inner = new StringJoiner(" ");
            for (int j = 1; j <= this.width; j++) {
                inner.add("*");
            }
            outter.add(inner.toString());
        }
        return outter.toString();
    }
    

    Now, I'm using StringJoiner for simplicity (and because I'm lazy and it's just an amazing class), but you could equally get away with using StringBuilder, but you'd need to inject the additional properties yourself.

    Then calling System.out.println(rectangle.toString()); would actually print the return result from the draw method

    Runnable example...

    import java.util.StringJoiner;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            Rectangle rectangle = new Rectangle(5, 3);
    
            System.out.println(rectangle.toString());
        }
    
        public class Rectangle {
    
            int width;
            int height;
    
            public Rectangle(int width, int height) {
                this.width = width;
                this.height = height;
            }
    
        public String draw() {
            StringJoiner outter = new StringJoiner("\n");
            for (int i = 1; i <= this.height; i++) {
                StringJoiner inner = new StringJoiner(" ");
                for (int j = 1; j <= this.width; j++) {
                    inner.add("*");
                }
                outter.add(inner.toString());
            }
            return outter.toString();
        }
    
            public String toString() {
                return draw();
            }
        }
    }