Results 1 to 8 of 8
Thread: Compile error using LinkedList
- 09-24-2011, 04:21 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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.
-
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.
- 09-24-2011, 04:26 AM #3
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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.
-
Re: Compile error using LinkedList
- 09-24-2011, 04:36 AM #5
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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?
-
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.
- 09-24-2011, 04:51 AM #7
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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!
-
Re: Compile error using LinkedList
You're welcome. If you're going to be spending any time coding in Java, make the API your "best friend forever".
Similar Threads
-
Java Compile error
By pablo2002 in forum Java AppletsReplies: 8Last Post: 09-12-2010, 02:48 AM -
Compile Error
By gcorreageek in forum Advanced JavaReplies: 2Last Post: 09-08-2010, 05:23 AM -
compile error
By angryredantz in forum New To JavaReplies: 1Last Post: 01-23-2009, 10:44 PM -
Compile Error - Please Help!!
By AJ2009 in forum New To JavaReplies: 10Last Post: 01-04-2009, 03:59 PM -
compile error
By dirtycash in forum New To JavaReplies: 6Last Post: 12-12-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks