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:
CodePudding user response:
toString
implementations should return the string value that represents the object instead of printing it directly, as there's no guarantee that the caller of the method will want to print it. Imagine, for example, that you want to write the result of toString
to a file -- how would you do it if it just printed to the console and returned nothing?
The best way to go about this is to modify your implementation of toString
to build the asterisk rectangle as a string and return it without printing anything. But if you just want to solve this problem anyway, you can return an empty string instead of null.
CodePudding user response:
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();
}
}
}
CodePudding user response:
Easiest solution is to have your Draw()
return empty string return ""
to make sure the word "null" isn't printed.
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 "";
}