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.

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43

Thread: Presumably simple fix sets/maps

  1. #26
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    The linkedlist's line values.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  3. #28
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    I want to compare the objects(ints aka line numbers) in the linked list that are stored in the value location on the TreeMap.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  5. #30
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    It's useful when printing variables to add an ID String to identify what is printed:
      System.out.println("wordMap="+wordMap);
     ...
          System.out.println("pE entry="+entry); //<<<<<<<<<  pE entry=cat=[2]
    My print out of entry (see above) is different from yours. You must not be printing the full contents.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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=".

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  9. #34
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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?

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    Just talking about output for debugging purposes.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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]

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    It is working now?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    Unfortunately not. It is still printing out the second 3 for mouse.

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  15. #40
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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 "[#,#]"

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  17. #42
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Presumably simple fix sets/maps

    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.

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Presumably simple fix sets/maps

    How would I save the value itr.next()
    You save a value by assigning it to a variable.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Sets/Maps help needed
    By waltershc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2013, 02:07 PM
  2. A simple mistake I can't fix
    By xdx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 5th, 2012, 05:50 AM
  3. Simple Google Maps like Image viewer in Java
    By javaguy2020 in forum What's Wrong With My Code?
    Replies: 26
    Last Post: December 7th, 2011, 11:46 PM
  4. [SOLVED] Maps and adding to sets as values
    By mds1256 in forum Collections and Generics
    Replies: 3
    Last Post: March 26th, 2010, 09:12 AM
  5. Convert Maps of String to Map of Maps
    By abhay8nitt in forum Collections and Generics
    Replies: 1
    Last Post: October 27th, 2009, 07:27 AM