I am unsure as to why I'm getting a dead code warning for my for loops, and I also don't know why only 1 of the symbols is printed, rather than the desired rectangle.
public static String rectangle(int row, int col, char thing) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
String character = "" + thing;
return character;
}
return "\n";
}
return "";
}
I tried putting the String character above the for loops, but still the same error. I cannot use any System.out.print in my code either. I tried int i = 1; i <= row, also tried it without the return ""; but I get an error for "no string returned."
The first
returnwill terminate the method on the first iteration of the loop. Instead of all those returns, you should append to the string and only return at the end:Having said that, using a
StringBuilderwould probably be more performant: