title: Hadoop MapReduce for loop
body: <p>I have a variable <code>_b</code>. I am adding cumulatively, but with each iteration of the for loop of <code>k</code>, the variable is getting reset to <code>0</code>. The same code works fine as a local Java application. Does anyone have any idea why this is happening? Here is the snippet of code from my program:</p>
  
  <pre><code>public void map(Object key, Text value, Context context)
  {
      int m, b, _m, _b;
  
      for (int i = 1; i &lt;= N; ++i)
      {
          for (int j = i; j &lt;= N; ++j)
          {
              _m = 0;
              _b = 0;
              for (int k = i; k &lt;= j; ++k)
              {
                  int _bTemp=0;
                  context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("Reset_bTemp=" + _bTemp + " ShouldNotReset_b=" + _b ));
                  /* Calculating smallM, _m and assigning it to global variable m */
                  _m += calculateSmallM(k,queryWord,value);
                  //context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("_m=" + _m));
                  /* Calculating smallB, _b and assigning it to global variable b */
                  _bTemp = calculateSmallB(k,value);
                  _b += _bTemp;
                  context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("_bTemp=" + _bTemp + "_b=" + _b ));
              }// end of for k
  
              /* Assigning _m and _b to m and b */
              m = _m;
              b = _b;
  
              /* Calculating Discrepancy Score */
              discrepancyScore = calculateDiscrepancyScore(m,b,M,B);
  
              if (discrepancyScore &gt; dmax)
              {
                  dmax = discrepancyScore;
                  iMax = i;
                  jMax = j;
  
              }
  
          }// end of for j
  
          hotspot_interval = Double.toString(dmax) + "=(" + Integer.toString(iMax) + "," + Integer.toString(jMax) + ")-(" + Integer.toString(i) + "," + Integer.toString(j) + ")";
          hotspot.set(hotspot_interval);
  
      }// end of for i 
  }
  
  public int calculateSmallB(int k, Text value, Context context) throws IOException, InterruptedException
  {
      int colon;
      String line = null;
      String tpifreqList = null;
      String lineWord = null;
      StringTokenizer st = new StringTokenizer(value.toString(),"\n");
      line = st.nextToken();
      //while (st.hasMoreTokens()) 
      //while((line = st.nextToken()) != null)
      while(line != null)
      {
          //line = st.nextToken();
          colon = line.indexOf("::");
          lineWord = line.substring(0, colon);
          tpifreqList = line.substring(lineWord.length() + 2); 
          /* lineWord.length()+1 will give the string starting with ":" */
          context.write(new Text(lineWord), new Text("Value of _b just after while(" + k + ")=" + smallB));
  
          /* trimming to remove whitespaces */
          tpifreqList = tpifreqList.trim();
          String[] tpiFreq = tpifreqList.split(","); /* Splitting to get a array of tpindex-freq values */
          /* Iterating through the array to retrieve and sum up all the frequencies for the 'word' occurring at tpindex k */
          for (int n = 0; n &lt; tpiFreq.length; n++) 
          {
  
              tpiFreq[n] = tpiFreq[n].trim();
              /* Checking if the tpindex is equal to k */
              if (tpiFreq[n].indexOf(Integer.toString(k)) == 0) 
              {
                  /* If yes, retrieve the frequency */
                  int hypen = tpiFreq[n].indexOf("-");
                  int freq = Integer.parseInt(tpiFreq[n].substring(hypen + 1));
                  //_b += freq;
                  smallB += freq;
                  context.write(new Text(lineWord), new Text("calculateSmallB(" + k + ")=" + smallB));
              }
          }
          context.write(new Text(lineWord), new Text("_b just after for(" + k + ")=" + smallB));
          line = st.nextToken();
      }
      return smallB;
  }
  </code></pre>