Question

I am trying to convert the output of this code into one JOptionPane message box. The code generates a 20x20 box of used to contain a "turtle" that a user can move around within the box that works along with an array to keep the turtle within the box boundaries. The "*" is left behind by the turtle when the user tells it to draw a path.

public void display() {
    for (int i = 0; i < colSize+2; i++)
        System.out.print("–");
    System.out.println();
    for (int i = 0; i < rowSize; i++) {
        System.out.print("|");
        for (int j = 0; j < colSize; j++) {
            if (i == currentRow && j == currentCol)
                System.out.print("T");
            else if (floor[i][j] == 0)
                System.out.print(" ");
            else System.out.print("*");
        }
        System.out.println ("|");
    }
    for (int i = 0; i < colSize+2; i++)
        System.out.print("–");
    System.out.println();
}

I've looked at a few threads on Stack Overflow about using StringBuilder with JOptionPane, but I can't figure out how to use multiple StringBuilders to work in one single JOptionPane message box. If anybody can help, it is greatly appreciated!

Was it helpful?

Solution

You state:

but I can't figure out how to use multiple StringBuilders to work in one single JOptionPane message box.

Use just one StringBuilder, not multiple. Where you see System.out.println(...) instead append(...) to your StringBuilder. For example,

StringBuilder sb = new StringBuilder();
for (int i = 0; i < something.length; i++) {
  sb.append(/* something */ + "\n");
}
// ... etc...

JOptionPane.showMessage(null, sb.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top