Results 1 to 8 of 8
  1. #1
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default Compile error using LinkedList

    Essentially this code reading an input file and inserting each line read into a linkedlist. If a line being read has already been read before (a duplicate line in the file), print the line being duplicated and remove the first occurrence of that. Here's what I've done so far.



    public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    Queue<String> s = new LinkedList<String>();
    String line;
    int n = 0;
    while ((line = r.readLine()) != null) {
    if (s.contains(line))
    {
    System.out.println(line);

    s.removeFirstOccurrence(line);

    }
    s.add(line);

    n++;
    }

    }

    It works great except for the "s.removeFirstOccurrence(line);" which gives a compile error of:

    cannot find symbol method removeFirstOccurrence(java.lang.String)

    I have no idea why it's doing this, it's a perfectly good method that can be used with linked lists but for some reason doesn't want to work.

    Any ideas why?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Compile error using LinkedList

    The method works for a LinkedList but the variable is a Queue variable, and this method simply does not exist for Queue.

  3. #3
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default Re: Compile error using LinkedList

    Thanks for the reply.

    I changed it to a List variable:
    List<String> s = new LinkedList<String>();

    But it still doesn't work. It gives the same error.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Compile error using LinkedList

    Quote Originally Posted by dre View Post
    Thanks for the reply.

    I changed it to a List variable:
    List<String> s = new LinkedList<String>();

    But it still doesn't work. It gives the same error.
    Before you asked this question, you've checked the List API to see if it has this method, right? You didn't ask without looking first, right?

  5. #5
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default Re: Compile error using LinkedList

    I didn't, but upon looking at the API it doesn't seem to exist under the List API.

    But it does exist under the LinkedList API, so I'm still not clear as to why it is not working. Which variable do I need to use if not Queue or List to make this work?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Compile error using LinkedList

    Again look at the API. If you must declare the variable as a super type and not a LinkedList type (which of course would work, by the way), the LinkedList API section on the method will tell you what super type declared it.

  7. #7
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default Re: Compile error using LinkedList

    Got it to work. I'm just learning about the Java Collections Framework so wasn't aware the API told me what to declare it as.

    Thanks for pointing that out, cheers!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Java Compile error
    By pablo2002 in forum Java Applets
    Replies: 8
    Last Post: 09-12-2010, 02:48 AM
  2. Compile Error
    By gcorreageek in forum Advanced Java
    Replies: 2
    Last Post: 09-08-2010, 05:23 AM
  3. compile error
    By angryredantz in forum New To Java
    Replies: 1
    Last Post: 01-23-2009, 10:44 PM
  4. Compile Error - Please Help!!
    By AJ2009 in forum New To Java
    Replies: 10
    Last Post: 01-04-2009, 03:59 PM
  5. compile error
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-12-2007, 06:00 PM

Posting Permissions

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