0

This is an extension of my previous question How to add values in a multi map

I was able to add the values in a multi map but the problem is when it finds duplicate keys it should add the values corresponding to its keys. Then once the current key changes to another number it should make the sum =0.00 and then start over again with a different duplicate key number. In the following code I don't know where to make the sum=0.00 and where to print the value so it only prints out key with the summed up value.

Here is the code (UPDATED CODE):

    // create a Map()
 Map<String, String> readValuemap = new LinkedHashMap<String,String>();
  String val = null;
  for(int i =0; i< 256;i++){
       for(int y=0; y< 256; y++){

             //reading in the values.
            String x = image.getLocationAsString(i, y);
        String n = image.getValueAsString(i, y);

        //Parsing them into "key" and "value".
        String delim = ", value=";
        String [] tokens = n.split(delim);
        double num = Double.parseDouble(tokens[1]);
        String stringNum = String.valueOf(num);

        String [] t = x.split("r=");
        String[] b = t[1].split(" mm/c");
        //System.out.print("Meet b:    "+b[0]);
        double radius = Double.parseDouble(b[0]);
        String stringRad = String.valueOf(radius);
        //System.out.println("The radius:   "+radius);


        //retrieve the current value for the key from the map.
        val = readValuemap.get(radius);
        System.out.println(val);
        //if null, just put the value into the map.
        if(val == null){
             readValuemap.put(stringRad, stringNum);
             System.out.println("new if; "+val);

        }
        else{   
             //if not null, add the current value to the new value (the one that you just read in) 
                       //and store the sum in the map.

        double v = Double.parseDouble(val);
        v += num;
        String newValue = String.valueOf(v);
        System.out.println("new value; "+newValue);
        readValuemap.put(stringRad, newValue);

        }

      }
    }

            System.out.println("-------------Printing out the values----------------");
            Iterator iter = readValuemap.entrySet().iterator();
            while(iter.hasNext()){
                Map.Entry pairs = (Map.Entry)iter.next();
                System.out.println(pairs.getKey() + " = "+ pairs.getValue());
            }

So basically it should be like this: the multimap contains:

1.36 = 59.0
1.36 = 65.0
1.35 = 56.0
1.35 = 71.0
1.34 = 64.0
1.34 = 75.0
1.33 = 59.0

Afterwards it should be like this (it should find any duplicate keys in the multimap and add the values):

1.36 = 124.0
1.35 = 127.0
1.34 = 139.0
1.33 = 59.0

Write now its just adding all the values regardless if the key is a duplicate or not.

Community
  • 1
  • 1
selena
  • 151
  • 13

1 Answers1

0

It appears that your solution is flawed; you have no need of a multi map. Try this:

  1. create a Map() (where something is Double, Float, or what ever you are using as the sum).
  2. read in one line (for example 1.36 = 65.0).
  3. parse it into key and value (for example key:1.36, value:65.0)
  4. retrieve the current value for the key from the map.
  5. if null, just put the value into the map.
  6. if not null, add the current value to the new value (the one that you just read in) and store the sum in the map.
  7. repeat until no more input.

Edits

  1. Don't store the sum as a string; store it as a Double in the map.
  2. You probably don't need the parsedouble on the radius; just store the value from b[0] as the radius. For example, stringRad = b[0];
  3. Store and retrieve using the same primitive type. You are putting using a String (stringRad) and getting using a double (radius).
  4. System.out.println("new if; "+val); will never be valuable in the if val == null block. Try + stringRad or + stringNum instead of +val.
DwB
  • 37,124
  • 11
  • 56
  • 82
  • So I did as you suggested (you can see the code in my updated question) but it doesn't seem to add the values instead it replaces it. What am I doing wrong here? – selena Jun 10 '14 at 20:21