0
public class AVLTree
{
   public static String inorderTraversal = " ";

   private static void inorder(AVLNode btree)
    {
       if (btree != null)
       {
         inorder(btree.left);
         inorderTraversal += btree.value + " ";
         inorder(btree.right);
       }
    } 

    /**
       This inorder method is the public interface to
       the private inorder method. It calls the private 
       inorder method and passes it the root of the tree.
    */

    public static void inorder()
    {
        inorder(root);
    }
}

class AVLTreeDemo extends JFrame
implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {
        String cmdStr = cmdTextField.getText();
          int size = Integer.parseInt(cmdStr);
        int[] array = new int[size];

        // input validation


        // Random number method
          randNum(array, size);

        // for loop adds numbers to AVL Tree        
        for (int i = 0; i < size; i++)
        {
            int value = array[i];
            avlTree.add(value);
        }
          if (view != null)
                remove(view);
            view = avlTree.getView();            
            add(view);
            pack();
            validate(); 
            cmdResultTextField.setText(" ");

        // inorder method
        AVLTree.inorder();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < size; i++)
        {
            sb.append(String.format(" %2d", size)); // Formats right justified
            if ((i + 1) % 10 == 0)
            {
                sb.append(System.lineSeparator()); // Adds a new line after 10 values
            }
        }
        //inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal));

        // display the array in inorder to the inorder text field
        inorderTextArea.setText(AVLTree.inorderTraversal);
    } 

    /**
      The randNum method randomly selects numbers
      in a given range.
      @param array The array.
      @param num The integer numbers.
   */

     public static void randNum(int[] array, int num)
     {                
        Random rand = new Random();

        // Selection sort method
          selectionSort(array);

        // display duplicates
        /*int last = array[0];
        int count = -1;

        for (int i : array)
        {
            if (i == last)
            {
               count++;
               continue;
            }
            System.out.println("Number " + last + " found " + count + " times.");
            count = 1;
            last = i;
         }*/

        for(int i = 0; i < num; i++)
        {
           // display duplicates
           /*if(num == num - 1)
           {
               System.out.print("Duplicate: " + num);
           }*/

           array[i] = rand.nextInt(500);
        }        
    }

    public static void main(String [ ] args)
    {
       AVLTreeDemo atd = new AVLTreeDemo();
    }
}

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 the 'for' loop I've provided, but I'm getting a compile error. The problem is with inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal));.

Error:

AVLTree.java:527: error: no suitable method found for toString(String) inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal)); ^ method StringBuilder.toString() is not applicable (actual and formal argument lists differ in length) method AbstractStringBuilder.toString() is not applicable (actual and formal argument lists differ in length) method Object.toString() is not applicable (actual and formal argument lists differ in length) 1 error

How can I re-write this line of code to make it work? Thank you for all your help.

Jeremy
  • 113
  • 2
  • 14
  • Please post the error as well. – Tanmay Patil Apr 25 '15 at 01:58
  • The error message seems quite clear to me. What don't you understand about it? – meriton Apr 25 '15 at 02:08
  • @meriton Yes, the error message is clear, but how do I re-write the line of code to make it work? – Jeremy Apr 25 '15 at 02:10
  • What is the AVLTree.inorderTraversal? Array? Object? Collection? – StanislavL Apr 25 '15 at 05:19
  • @StanislavL I didn't put all of that code in here because it's over 600 lines of code, and I've been told by another programmer in here to not submit so much code. I'll try to submit just the parts that pertain to the problem. Thank you! – Jeremy Apr 25 '15 at 13:12

0 Answers0