title: Convert string to long in java
body: <p>I am working with a spring boot project and I am trying to create a route in the controller that works with a dynamic number of parameters. The route looks like this:</p>
  <pre><code>@RequestMapping(value = &quot;/addItem&quot;, method = RequestMethod.POST)
      public String addItem(ModelMap model, @RequestParam Map&lt;String,String&gt; allRequestParams) {
          String stringId = allRequestParams.get(&quot;id&quot;);
          System.out.println(stringId);
          long id = Long.valueOf(stringId);
          System.out.println(id);
          ...
      }
  </code></pre>
  <p>I need to convert the id parameter to a <code>Long</code> and I have tried to do this with <code>Long.valueOf(stringId)</code> and <code>Long.parseLong(stringId)</code> but when I call the route with the parameter <code>id</code> as 1, I get this error:</p>
  <pre><code>Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: For input string: &quot;1
  &quot;] with root cause
  
  java.lang.NumberFormatException: For input string: &quot;1
  </code></pre>
  <p>I call the route from a form like this:</p>
  <pre><code>&lt;form method=&quot;POST&quot; action=&quot;addItem&quot;&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;table id=&quot;details&quot; class=&quot;table table-responsive&quot;&gt;
                      &lt;tr id=&quot;type&quot;&gt;
                          &lt;th class=&quot;text-left select-title&quot;&gt;Type* :&lt;/th&gt;
                          &lt;td&gt;
                              &lt;select name=&quot;id&quot; id=&quot;input&quot; class=&quot;form-control form-control-sm inv-select&quot; required&gt;
                                  &lt;option value=&quot;&quot; disabled selected&gt;Selected...&lt;/option&gt;
                                  &lt;%@ include file = &quot;common/dropdown.jsp&quot; %&gt;
                              &lt;/select&gt;
                          &lt;/td&gt;   
                      &lt;/tr&gt;
                      &lt;tr id=&quot;component1&quot;&gt;
                          &lt;th class=&quot;text-left select-title&quot;&gt;Type of Component * :&lt;/th&gt;
                          &lt;td&gt;
                              &lt;select id=&quot;select&quot; name=&quot;component1&quot; class=&quot;form-control form-control-sm inv-select&quot; required&gt;
                                  &lt;option value=&quot;&quot; disabled selected&gt;Selected...&lt;/option&gt;
                                  &lt;%@ include file = &quot;common/dropdown.jsp&quot; %&gt;
                              &lt;/select&gt;
                          &lt;/td&gt;
                          &lt;th class=&quot;text-left quant-title&quot;&gt;Quantity * :&lt;/th&gt;
                          &lt;td class=&quot;quantity-field&quot;&gt;
                              &lt;input name=&quot;quantity1&quot; type=&quot;number&quot; step=&quot;any&quot; placeholder=&quot;Quantity&quot; class=&quot;quantity-input&quot; required&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr class=&quot;table-row&quot;&gt;
                          &lt;td&gt;
                              &lt;button onclick=&quot;addComponent()&quot; id=&quot;add-component&quot; class=&quot;btn btn-md btn-outline-success&quot;&gt; + Add Component&lt;/button&gt;&lt;br&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;button onclick=&quot;removeComponent()&quot; id=&quot;rem-component&quot; class=&quot;btn btn-md btn-outline-danger&quot;&gt; - Remove Component&lt;/button&gt;&lt;br&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;th&gt;
                              &lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot;&gt;
                          &lt;/th&gt;
                      &lt;/tr&gt;
                    &lt;/table&gt;
                  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>The print statements in the controller route are there for debugging and only the first print statement prints. How can I correctly convert the string to a long?</p>