Results 1 to 3 of 3
  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Compiler issue? syntax error on token ( expression expected after this token treeset

    Hi All,

    I am revisiting Collections and doing some exercise in the book. Having trouble running code in Eclipse so need your help guys. Here is my code:

    Java Code:
    package chap16;
    
    import java.util.*;
    
    public class TestTree {
    
    	public static void main(String[] args) {
    		new TestTree().go();
    
    	}
    	public void go(){
    		Book1 b1 = new Book1("How Cats Work");
    		Book1 b2 = new Book1("Remix your Body");
    		Book1 b3 = new Book1("Finding Emo");
    		
    		TreeSet<Book1> tree = TreeSet<Book1>();
    		tree.add(b1);
    		tree.add(b2);
    		tree.add(b3);
    		System.out.println(tree);
    	}
    }
    
    class Book1 implements Comparable{
    	String title;
    	
    	public Book1(String t){
    		title = t;
    	}
    	
    	public String toString(){
    		return title;
    	}
    
    	public int compareTo(Object b) {
    		Book1 book = (Book1)b;
    		return (title.compareTo(book.title));
    	}
    }
    TreeSet<Book1> tree = TreeSet<Book1>(); line gives me error and I dont really know what is the issue here. I tried to search for similar questions and answers and no luck so far. First thought was compiler maybe outdated but I have jdk 1.7, also tried to run from command line which gives me:
    Java Code:
    TestTree.java:16: error: illegal start of expression
                    TreeSet<Book1> tree = TreeSet<Book1>();
                                                        ^
    Am I missing something here? Can you tell what I am doing wrong guys, any suggestion will be greatly appreciated. Thank you!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Compiler issue? syntax error on token ( expression expected after this token tree

    Make that line: TreeSet<Book1> tree = new TreeSet<Book1>();

    Also, make your Book1 class implement the Comparable<Book1> interface (not the 'raw' interface).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Compiler issue? syntax error on token ( expression expected after this token tree

    Dumb me! Can't believe I didn't use "new" when creating treeset, very elementary stuff...Thank you for reply. Regards,

Similar Threads

  1. Replies: 3
    Last Post: 03-09-2014, 08:57 AM
  2. Syntax error on token "(", @ expected and more errors
    By ebrown086 in forum New To Java
    Replies: 4
    Last Post: 08-17-2013, 06:31 PM
  3. Syntax error on token "}", super expected
    By amentjacob1 in forum New To Java
    Replies: 9
    Last Post: 07-06-2012, 05:15 AM
  4. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 01:19 AM
  5. Syntax error on token "(", ; expected
    By romilc in forum New To Java
    Replies: 7
    Last Post: 10-24-2009, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •