The linkedlist's line values.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
The linkedlist's line values.
What objects are those in? You need object names to write code.
Use println() calls to show the following:
What is in the collection: wordMap?
What is in the object: entry? The code only prints part of it.
If you don't understand my answer, don't ignore it, ask a question.
I want to compare the objects(ints aka line numbers) in the linked list that are stored in the value location on the TreeMap.
How can you get references to each of those objects so they can be compared?
What printed when you used println() calls to show the following:
What is in the collection: wordMap?
What is in the object: entry? The code only prints part of it.
If you don't understand my answer, don't ignore it, ask a question.
wordmap is line 2. the values for each entry are above their respective entries.
Running.
[cat=[2], mouse=[3, 3], phone=[4, 5], string=[1]]
[2]
ID: cat Lines: 2 Occurance: 1
[3, 3]
ID: mouse Lines: 3, 3 Occurance: 2
[4, 5]
ID: phone Lines: 4, 5 Occurance: 2
[1]
ID: string Lines: 1 Occurance: 1
It's useful when printing variables to add an ID String to identify what is printed:My print out of entry (see above) is different from yours. You must not be printing the full contents.System.out.println("wordMap="+wordMap); ... System.out.println("pE entry="+entry); //<<<<<<<<< pE entry=cat=[2]
If you don't understand my answer, don't ignore it, ask a question.
What is your suggestion? I must be missing the point. I do not see how my printout is different from yours except for the concatenation of "wordMap=" and "pE entry=".
The point is raw data without an id String can be hard to associate with the variable that held it.
My entry printout: pE entry=cat=[2]
your printout: [2]
Your printout doesn't say what [2] is? It's not the value of an entry, its some contents of the entry object.
If you don't understand my answer, don't ignore it, ask a question.
So you are saying what needs to be inserted into the linked list needs to be a string including the line number instead of an integer? ...Or are we just talking about output for debugging purposes?
Just talking about output for debugging purposes.
If you don't understand my answer, don't ignore it, ask a question.
Output:
wordMap=[cat=[2], mouse=[3, 3], phone=[4, 5], string=[1]]
pE entry=[2]
ID: [cat] Lines: 2 Occurance: [1]
pE entry=[3, 3]
ID: [mouse] Lines: 3, 3 Occurance: [2]
pE entry=[4, 5]
ID: [phone] Lines: 4, 5 Occurance: [2]
pE entry=[1]
ID: [string] Lines: 1 Occurance: [1]
It is working now?
If you don't understand my answer, don't ignore it, ask a question.
Unfortunately not. It is still printing out the second 3 for mouse.
Are the 3s adjacent to each other in the list? After outputting one item from the list can you check the next item for equality and skip outputting it if it matches the last item output?
Use variables to hold the values and compare before output.
If you don't understand my answer, don't ignore it, ask a question.
I have tried various forms of that
//local occurance variable int occurance = 1; String check = ""; //System.out.println("pE entry=" + entry.getValue().toString()); //print the word and the line numbers as well as test for duplicate line integers on the same key Iterator itr = ((LinkedList) entry.getValue()).iterator(); System.out.print("ID: [" + entry.getKey() + "] Lines: " + itr.next()); while(itr.hasNext()){ occurance++; //check = itr.toString(); if(itr.equals(itr.next())){ //what i assumed would work System.out.println("blah"); } else{ System.out.print(", " + itr.next()); //no such element exception } } //prints occurance from incremented occurance variable System.out.print(" " + " Occurance: [" + occurance + "]"); System.out.println(); }
I have also tried declaring a string variable to compare the itr.toString() and itr.next().toString() but after further testing, different line numbers result in the same string, this made no sense to me. I just added a .toString() method at the end of my line number printer.
I tried making an integer variable to store the previous value but had a type mismatch error converting from object to int. this part confused me the most as I know I am pulling a value from inside an object.
But to answer your question, they are next to each other in the list. I don't know EXACTLY what separates them but when I print out the map entries or the entry values they are both listed in the format "[#,#]"
Where does the code save items from the list so they can be checked?
For example this line prints out an item without saving it for checking:System.out.print("ID: [" + entry.getKey() + "] Lines: " + itr.next());
If you don't understand my answer, don't ignore it, ask a question.
There is currently no point in my code that saves an item for checking. How would I save the value itr.next() prints? I cannot make some integer variable and just assign it to equal itr.next() as my IDE gives me a cannot convert from object to int error.
You save a value by assigning it to a variable.How would I save the value itr.next()
If you don't understand my answer, don't ignore it, ask a question.