title: Updating specific part of value in hash map dynamically
body: <p>I have a spring boot app with a HTTP post request handler. It accepts a payload that I parse and outputs a JSON. I have handled it that it needs to accept a payload of certain parameters(18).</p>
  
  <pre><code>import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.validation.annotation.Validated;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.google.gson.Gson;
  
  
  
  
  @Validated
  @RestController
  public class MockController {
  
  
      @Autowired
      MockConfig mockconfig;
  
      private static final Logger LOGGER = LoggerFactory.getLogger(MockController.class);
  
  
  
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String index() {
          return "hello!";
      }
          String[] parse;
      @PostMapping(value = "/")
      public String payloader(@RequestBody String params ) throws IOException{
          LOGGER.debug("code is hitting");
          parse = params.split("\\|"); 
          String key;
          String value;
          String dyn;
          Map&lt;String, String&gt; predictionFeatureMap = mockconfig.getPredictionFeatureMap();
  
          if(parse.length!=18) {
  
              key = "Not_enough_parameters";
              value = predictionFeatureMap.get(key);
              Map&lt;?, ?&gt; resultJsonObj = new Gson().fromJson(value, Map.class);
  
          }
          else {
              key = params;
              value = predictionFeatureMap.get(key);
          }
          return value;
      }
  }
  </code></pre>
  
  <p>My config class is where I get the input and output from a file and put them into a hashmap. </p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Scanner;
  
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  public class MockConfig {
      private Map&lt;String, String&gt; predictionFeatureMap = new HashMap&lt;String, String&gt;();
  
      public Map&lt;String,String&gt; getPredictionFeatureMap() throws IOException {
          return predictionFeatureMap;
      }
  
      public MockConfig() throws IOException {
          init();
      }
  
      private Map&lt;String, String&gt; init() throws IOException {
          Scanner sc = new Scanner (new File("src/test/resources/Payload1.txt"));
  
          int counter = 1;
          String key = "";
          while (sc.hasNextLine()) {
              if(counter % 2 != 0) {
                  key = sc.nextLine();
              }else {
                  predictionFeatureMap.put(key, sc.nextLine());
              }
              counter++;
          }
          sc.close();
          return predictionFeatureMap;
      }
  
  
  
  
  }
  </code></pre>
  
  <p>This is the key and value in the file that I'm trying to work with specifically. </p>
  
  <pre><code>Not_enough_parameters
  </code></pre>
  
  <pre><code>{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting: 18, actual:8", "version": "0.97", "coreName": "Patient_Responsibility"}
  </code></pre>
  
  <p>(The JSON string is the response to too much or too little parameters... the paramter length should be 18.)</p>
  
  <p>Example input:</p>
  
  <pre><code>ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0
  </code></pre>
  
  <p>This input would pass because it has 18 parameters...</p>
  
  <p>What I want to do is if a user curls for example 5 parameters</p>
  
  <pre><code>ncs|56-2629193|1972-03-28|20190218|77067
  </code></pre>
  
  <p>I want the value(JSON error message) to have the 'actual' field updated like:</p>
  
  <pre><code>{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting: 18, actual:5", "version": "0.97", "coreName": "Patient_Responsibility"}
  </code></pre>
  
  <p>without hardcoding it into the txt file or hashmap...</p>
  
  <p>I have tried getting the index of the string and replacing the '8' character with parse.length() and casting it as a char but it just gives me this output: </p>
  
  <pre><code>{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting:1 actual:", "version": "0.97", "coreName": "Nulogix_Patient_Responsibility"}
  </code></pre>
  
  <p>How do I parse or index the JSON to update this value? Or is there a hashmap method to deal with this?</p>