I want to return a String/StringBuilder of a large letter E that is filled in with E's such that I get something like this:
EEEEEE
E
E
E
EEEEEE
E
E
E
EEEEEE
The issue I am having right now is that I have a StringBuilder value, sb, that should be getting updated during the nested for loop. What do I have to do to have my function drawLetters return sb so that I can print it in my main() function?
The error I get is drawLetters is missing a return statement. I tried to move the sb outside of the for loop, but then the for loop doesn't update the variable because it is outside of the for loop scope.
tl;dr - confused about scope and where the sb variable should go so that my function drawLettersreturns sb
public class MCVE {
public static String drawLetters(String string) {
// Create BufferedImage object called "background".
BufferedImage background = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);
// Creates a Graphics2D object called "g" by calling the method get.Graphics() on the background
Graphics g = background.getGraphics();
// This decides what is drawn.
g.drawString(string, 6, 12);
StringBuilder sb = new StringBuilder();
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 144; x++)
sb.append(background.getRGB(x, y) == -16777216 ? " " : background.getRGB(x, y) == -1 ? string : "*");
if (sb.toString().trim().isEmpty())
continue;
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(drawLetters("E"));
//drawLetters("J");
//drawLetters("K");
}
}
sboutside of the loop if you plan to return it outside of the loopsb.toString().trim().isEmpty()is always true, thereturnwill never be reached. I'm not sure what the potential for that happening is.return sb.toString();, it makes more sense to return the actual value