The linkedlist's line values.
Printable View
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.
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.
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.Code :
System.out.println("wordMap="+wordMap); ... System.out.println("pE entry="+entry); //<<<<<<<<< pE entry=cat=[2]
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.
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.
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?
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.
I have tried various forms of that
Code java:
//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:Code :
System.out.print("ID: [" + entry.getKey() + "] Lines: " + itr.next());
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.Quote:
How would I save the value itr.next()