Results 1 to 7 of 7
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default HashMap + make a sum of it.

    Hi,

    I am finishing up a program becose I am going on vacation next week. However there is still one thing I can't get to work. Even though I already asked help here. But that topic is already 'old' now. So here we go again.

    I have a HashMap, and now I got a for loop to look up the string (AFVDSQ) in HashMap. Now these 'numbers' respresent a value. And I would like to get the A * F * V * D * S * Q. But i don't know how to do that. And all my attemps fail...

    Java Code:
     
    					for (int i = 0; i < peptide.length(); i++) {
    					   String ss = peptide.substring(i, i +1);
    					   System.out.println(y.get(ss));
    					}
    peptide = string with the letters.
    Last edited by Lund01; 07-01-2011 at 07:40 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I have a HashMap, and now I got a for loop to look up the string (AFVDSQ) in HashMap.
    What does the Map store (it helps to define it at least someone in your posted code)?
    Now these 'numbers' respresent a value
    What numbers and what value? Do you mean the character integer value? The hashcode? A defined value in a Map? I recommend you explain your problem in much more detail.

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I have a file and in there there are certain Strings.
    Like: AVV or AQWRTESAVGFS.
    Now I have created a Hashmap, below is a part of it.

    Java Code:
     
    Map<String, String> y = new HashMap<String, String>();
    y.put("A", "71.0788");
    y.put("R", "156.1875");
    And in this 'for' loop it looks at the string and takes out the number from Hashmap.
    Like an A represents the value: 71.0788.
    What I would like is that the string (example) ARR, becomes: 71.0788 * 156.1875 * 156.1875 = ......(answer I need)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Get the value String for each letter from the HashMap, use a parse method (See Double) to convert the String to a number and multiply those numbers to get the product you need.

    becomes: 71.0788 * 156.1875 * 156.1875
    Is that at String: "71.0788 * 156.1875 * 156.1875"
    or does it represent a product of the three numbers?

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    This is the String we have: ARR
    This is what I still need to add: 71.0788 * 156.1875 * 156.1875 = ......(end product I need)

    These strings can be 3 letters long or 25.
    There is not a solid length.
    Also with this code I can look them up:
    Java Code:
     
    					for (int i = 0; i < peptide.length(); i++) {
    					   String ss = peptide.substring(i, i +1);
    					   System.out.println(y.get(ss));
    
    					}
    But I still don't get how I should each letter to * with the next.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Define a variable before the loop equal to a value (say 1 for multiplication, 0 for addition), and through each iteration use Norm's advice to get the value from the map, parse it to double, then perform the math operation on the value defined prior to the loop.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I still don't get how I should each letter to * with the next.
    Use concatenation to build the String with the *
    String prod = "<FirstNumber>"
    for (...) {
    prod += "*" + "<NextNUMBER>";
    }

    then prod will be "<FirstNumber> * <NextNUMBER> * <NextNUMBER> * ... "

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  2. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 03:29 PM
  3. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 01:55 PM
  4. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:35 PM
  5. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 03:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •