0
for(int i=0; i < list.length; i++)
{
    System.out.printf(" %2d",list[i]); // Formats right justified

    if ((i+1)%10==0)
    {
        System.out.println(); // Adds a new line after 10 values
    }
}

I'm trying to display the output of an AVL Tree inorder, preorder, and postorder traversals on multiple lines in the JTextArea(), preferably 10 numbers to a line. I tried it in the JTextArea() by adding 5 and 10 to the JTextArea() parameter (JTextArea(5, 10)), but it doesn't work. If so, how can the 'for' loop provided be altered to accomplish this? Thank you for all your help.

Jeremy
  • 113
  • 2
  • 14

1 Answers1

0

I think you need something like an (efficient) "string aggregating object":

StringBuilder sb = new StringBuilder();//before your loop

(String + String + String ..is considered ineffcient, but also possible)

In your loop, you populate sb:

for (int i = 0; i < list.length; i++) {
    sb.append(String.format(" %2d", list[i])); // Formats right justified
    if ((i + 1) % 10 == 0) {
        sb.append(System.lineSeparator()); // Adds a new line (windows or unix like) after 10 values
    }
}

(@see StringBuilder, String.format, System.lineSeparator)

And after that you can set the text of the TextArea like:

myJTextArea.setText(sb.toString());
xerx593
  • 12,237
  • 5
  • 33
  • 64
  • I'm using Java programming language, I can't use 'append.' – Jeremy Apr 25 '15 at 00:15
  • 1
    Disregard my last comment, I can use 'append()' with the StringBuilder class. Thank you! – Jeremy Apr 25 '15 at 00:31
  • How can I use 'myJTextArea.setText(sb.toString());' to work with inorderTextArea.setText(AVLTree.inorderTraversal);? I tried inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal)); but I'm getting a compile error. – Jeremy Apr 25 '15 at 01:06
  • Assume `myJTextArea == yourTextArea` :-) (any of the 3!?), and consider `return sb.toString();` in each of your "xOrderTraversal" (as the last/return statement). – xerx593 Apr 25 '15 at 21:13