Results 1 to 12 of 12
  1. #1
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default How to get a single value from a Map?

    My problem starts off by reading this text file in:

    Every good boy does fine and
    the quick brown fox jumps over the lazy dog and

    We have to create a map and use each word as a key and what # the word is as the value. This is what should be printed out:

    Every 1, (Every is the first word, so it has a 1, next to it)
    and 6,16, (and is used twice as the 6th and 16th word, so it has 6, and 16,)
    boy 3,

    I have to use a HashMap<String, StringBuilder> to store the values, and I have no problems up to using the put(K,V) method. However, I don't know how to add a second value, such as 16 for and in the example above.

    Variables in my program:
    indexMap is the hashMap, String word is the key, and StringBuilder sb is the value.

    I can do indexMap.put(word, sb); to add it to the Map but when I do that, the key and value are replaced. I need the new value to be appended to the end of the current value. My plan was to get the value already stored, append it to the StringBuilder, and then append the new value I want to add to the end of that. The problem I'm having is in getting that single value that is in the Map, so basically I don't know how to get "6," when I want to add "16," to the end of it.

    This is what I wanted to do in code, except that getValue() doesn't work:

    //if the key already exists
    if(indexMap.containsKey(word))
    {
    //get the value in the word and append it to the StringBuilder
    sb.append(indexMap.getValue(word) + ",");

    //get the new value to add and append it to the end of StringBuilder
    sb.append((Integer.toString(count) + ",");

    //clear the existing key and map
    indexMap.remove(word);

    //put the key and the new value in
    indexMap.put(word, sb);

    }

    The core problem is how to append what numbered word it is to the current value in the spot, so if what I thought is completely off, I would appreciate if someone told me the right way to go about doing it =p. Thank you very much.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    The approach sounds OK. (But not how I would have done it: I would have made the values lists rather than string builders. Then just appended the incoming value. The business of formatting with commas I would have left to whoever gets the map value).

    Java Code:
    //if the key already exists
    if(indexMap.containsKey(word))
    {
    //get the value in the word and append it to the StringBuilder
    sb.append(indexMap.getValue(word) + ",");

    So what is sb - what is the StringBuilder to which you are appending?

    Rather than removing the old key/value pair and adding a new one it might be more straightfoward to get the existing StringBuilder value and append directly to that.

  3. #3
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The approach sounds OK. (But not how I would have done it: I would have made the values lists rather than string builders. Then just appended the incoming value. The business of formatting with commas I would have left to whoever gets the map value).

    Java Code:
    //if the key already exists
    if(indexMap.containsKey(word))
    {
    //get the value in the word and append it to the StringBuilder
    sb.append(indexMap.getValue(word) + ",");

    So what is sb - what is the StringBuilder to which you are appending?

    Rather than removing the old key/value pair and adding a new one it might be more straightfoward to get the existing StringBuilder value and append directly to that.
    I had the option of using a List of ints or a StringBuilder.. and I went with the StringBuilder because I have never used a list like that, should have thought about that more. sb is the number the counter was at converted into a StringBuilder, the counter increments every time the Scanner gets the next() word (so it would be at 16 while the current value stored for and is 6). So I could append it directly to the StringBuilder, but I didn't think you could append the StringBuilder straight to the value in the Map. How would I go about doing that?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    So I could append it directly to the StringBuilder, but I didn't think you could append the StringBuilder straight to the value in the Map. How would I go about doing that?

    Yes - the value in the map is a reference to a StringBuilder. You can't change that reference without removing the key/value pair and putting a new one in as you are doing now. But you can change the contents of the StringBuilder instance.

    So, inside the if block...

    Java Code:
    StringBuilder value = indexMap.getValue(word);
    value.append("whatever you like");
    // finished!

    Try this and see what happens. (You can expect problems with those pesky commas - but that's a separate issue ;)

  5. #5
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Try this and see what happens. (You can expect problems with those pesky commas - but that's a separate issue ;)
    I tried

    if(indexMap.containsKey(word))
    {
    StringBuilder value = indexMap.getValue(word);
    value.append((Integer.toString(count)));

    }


    and I got the "cannot find symbol" error message. So that leads me to think that I overlooked something obvious that you assumed was right, otherwise do you know what's wrong with that code? And I ignored the commas for now lol.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    I got the "cannot find symbol" error message. So that leads me to think that I overlooked something obvious that you assumed was right

    Exactly.

    I assumed getValue() was right (because you had it in your code - well, that's my excuse). Actually the method should be get().

    So use get() instead of getValue().

    ---------------------

    A couple of points about using a forum like this:

    If you get a compiler message (that you can't understand), copy and post the whole thing. That saves guessing. Also it will refer to some line number in your code; since only you can see what line that is you should say in your post what line that is.

    When posting code use the code tags: basically you put [code] at the start of the code and [/code] at the end. That way the code will show up with the correct indentation etc.

  7. #7
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Exactly.

    I assumed getValue() was right (because you had it in your code - well, that's my excuse). Actually the method should be get().

    So use get() instead of getValue().

    ---------------------

    A couple of points about using a forum like this:

    If you get a compiler message (that you can't understand), copy and post the whole thing. That saves guessing. Also it will refer to some line number in your code; since only you can see what line that is you should say in your post what line that is.

    When posting code use the code tags: basically you put [code] at the start of the code and [/code] at the end. That way the code will show up with the correct indentation etc.
    Thanks for the help, it has really been useful. Sorry, it was the first time posting and I did not read through all the guidelines, sorry if I wasted your time. And I was so close to getting it right, depressing that I lost points for it =/ lol unfortunately it was the first time I used a map (the only example lab we had was for a list).

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    Sorry, it was the first time posting and I did not read through all the guidelines, sorry if I wasted your time.

    No problem - I only mentioned those things because they are nonobvious things that facilitate communication.

    Did you try

    Java Code:
    if(indexMap.containsKey(word))
    {
        StringBuilder value = indexMap.get(word);
        value.append((Integer.toString(count)));
    }

    What happened?

  9. #9
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    No problem - I only mentioned those things because they are nonobvious things that facilitate communication.

    Did you try

    Java Code:
    if(indexMap.containsKey(word))
    {
        StringBuilder value = indexMap.get(word);
        value.append((Integer.toString(count)));
    }

    What happened?
    I will get back to you on this, I appreciate it but its Friday Night!!! I will get back to thinking logicallsoon enough

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    Fair enough.

  11. #11
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Fair enough.
    Thanks for the help.. all I had to switch was getValue() to get(), when I was looking over all the methods I saw get(key k) and assumed it just returned a key, definitely wasn't thinking. We had to have a midterm, but a couple snow days threw off everything.. so I never actually used a map before and I wasn't familiar at all with the methods. Thanks dude I got it working now.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

Similar Threads

  1. Single sign on
    By Enthiran in forum Advanced Java
    Replies: 1
    Last Post: 01-14-2011, 03:01 PM
  2. Single instance
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 08-29-2010, 08:47 PM
  3. Single Sign on
    By iksaak in forum Advanced Java
    Replies: 0
    Last Post: 07-06-2010, 09:36 AM
  4. Single linked lists - help
    By Srcee in forum New To Java
    Replies: 10
    Last Post: 10-29-2009, 06:35 PM
  5. Single Component Padding
    By subb in forum AWT / Swing
    Replies: 2
    Last Post: 03-28-2009, 08:09 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
  •