Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading