Hey guys. I am trying to sort a list that has been populated using a random number generator. I am trying to use collections to sort the list but I am not have any luck. I have been stuck on this for a while and would greatly appreciate any help or pointers you could give me. I only attached my main class because I think you can tell what is going on from that. Everything works except line 22. This is the error I am getting:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at List.ListTest.main(ListTest.java:22)
Java Result: 1

Thanks in advance for any help!

import java.util.*;


public class ListTest
  {
     public static void main( String args[] )
     {
        List list = new List(); // create the List container


             Random generator = new Random();

     int value;

     for ( int i =1; i <= 25; i++)
     {
         value = generator.nextInt(100);
         list.insertAtFront( value );
         list.print();
     }

       Collections.sort(list);

    
       

     } // end main
  } // end class ListTest

The code you posted does not compile without errors.
Do you expect it to compile?
For example: List is an interface.

That is correct. I posted the error code in the initial post. "Uncompilable source code
at List.ListTest.main(ListTest.java:22)"

My question is why does this not work? Is Collections.sort the right way to sort a List?

The code does not compile. The error is not on line 22.

These methods are not found. What class are they in?

list.insertAtFront( value );
         list.print();

Comment out line 22 and try to compile the code.
What happens?

Those methods are here in List.java. Everything seems to work except line 22. If I remove line 22 the code compiles and runs fine. The trouble I am having is sorting the list. Any thoughts?

class ListNode
    {
       // package access members; List can access these directly
       Object data;
      ListNode nextNode;

      // constructor creates a ListNode that refers to object
      ListNode( Object object )
      {
         this ( object, null );
      } // end ListNode one-argument constructor

      // constructor creates ListNode that refers to
      // Object and to next ListNode
      ListNode( Object object, ListNode node )
      {
         data = object;
         nextNode = node;
      } // end ListNode two-argument constructor

      // return reference to data in node
      Object getObject()
      {
         return data; // return Object in this node
      } // end method getObject

      // return reference to next node in list
      ListNode getNext()
      {
         return nextNode; // get next node
      } // end method getNext
   } // end class ListNode

   // class List definition
   public class List
   {
      private ListNode firstNode;
      private ListNode lastNode;
      private String name; // string like "list" used in printing

      // constructor creates empty List with "list" as the name
      public List()
      {
         this( "list" );
      } // end List no-argument constructor

      // constructor creates an empty List with a name
      public List( String listName )
      {
         name = listName;
         firstNode = lastNode = null;
      } // end List one-argument constructor

      // insert Object at front of List
      public void insertAtFront( Object insertItem )
      {
         if ( isEmpty() ) // firstNode and lastNode refer to same object
            firstNode = lastNode = new ListNode( insertItem );
         else // firstNode refers to new node
            firstNode = new ListNode( insertItem, firstNode );
      } // end method insertAtFront

      // insert Object at end of List
      public void insertAtBack( Object insertItem )
      {
         if ( isEmpty() ) // firstNode and lastNode refer to same Object
            firstNode = lastNode = new ListNode( insertItem );
         else // lastNode's nextNode refers to new node
            lastNode = lastNode.nextNode = new ListNode( insertItem );
      } // end method insertAtBack

      // remove first node from List
      public Object removeFromFront() throws EmptyListException
      {
         if ( isEmpty() ) // throw exception if List is empty
            throw new EmptyListException( name );

         Object removedItem = firstNode.data; // retrieve data being removed

         // update references firstNode and lastNode
         if ( firstNode == lastNode )
            firstNode = lastNode = null;
         else
            firstNode = firstNode.nextNode;

         return removedItem; // return removed node data
      } // end method removeFromFront

      // remove last node from List
      public Object removeFromBack() throws EmptyListException
      {
         if ( isEmpty() ) // throw exception if List is empty
            throw new EmptyListException( name );

        Object removedItem = lastNode.data; // retrieve data being removed

        // update references firstNode and lastNode
        if ( firstNode == lastNode )
           firstNode = lastNode = null;
        else // locate new last node
        {
           ListNode current = firstNode;

           // loop while current node does not refer to lastNode
           while ( current.nextNode != lastNode )
              current = current.nextNode;

           lastNode = current; // current is new lastNode
           current.nextNode = null;
        } // end else

        return removedItem; // return removed node data
     } // end method removeFromBack

     // determine whether list is empty
     public boolean isEmpty()
     {
        return firstNode == null; // return true if List is empty
     } // end method isEmpty

     // output List contents
     public void print()
     {
        if ( isEmpty() )
        {
           System.out.printf( "Empty %s\n", name );
           return;
        } // end if

        System.out.printf( "The %s is: ", name );
        ListNode current = firstNode;

        // while not at end of list, output current node's data
        while ( current != null )
        {
           System.out.printf( "%s ", current.data );
           current = current.nextNode;
        } // end while

        System.out.println( "\n" );
     } // end method print




   

     }

A comment: It's better if you DO NOT USE one of Java's classnames for your own: List

Read the API doc for the Collections sort() method.
What data type does it take for an argument? Is your List class that type?

No, it is type integer. I see... Is there a sort that will uses type integer?

it is type integer

What is the it that is an int?

a sort that will uses type integer

I assume that you are talking about an array of int.
Look at the Arrays class it has several sort() methods.

I see. Thanks again for your help! Below is the main method I used to fix this.

import java.util.*;

public class ListTest
{
   public static void main( String[] args )
   {
        
       
        LinkedList< Integer > list1 = new LinkedList< Integer >();

       Random generator = new Random();

    

     for ( int i =1; i <= 25; i++)
     {
       
         int value;
         
         value = generator.nextInt(100);
         list1.add( value );
                    

     }

     for ( Integer list : list1 )
         System.out.println( list );


     Collections.sort(list1);


     System.out.println( "The sorted linkedlist is:" );
     for ( Integer list : list1 )
         System.out.println( list );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.