简体   繁体   中英

Having trouble with return variable scope in java

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 drawLetters returns 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");
    }
}

You declare sb inside the loop:

for (int y = 0; y < 32; y++) {
    StringBuilder sb = new StringBuilder();
    //...
} //Scope of sb ends here

So it is out of scope outisde of the outer for loop. Right now you are trying to return it inside the for loop, which works, but isn't what you want. Declare it outside of the loop:

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;

Also if you want to print sb , use the toString() method, and do it before you return; otherwise it is an unreachable statement.

If I were you, I would return a String , and not a StringBuilder . Otherwise everytime you call the method and try to print it you will have to add in a toString() call. You can change your method signature to:

public static String drawLetters(String string)

And then in your return statement

return sb.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM