简体   繁体   中英

Convert string to long in java

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:

@RequestMapping(value = "/addItem", method = RequestMethod.POST)
    public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) {
        String stringId = allRequestParams.get("id");
        System.out.println(stringId);
        long id = Long.valueOf(stringId);
        System.out.println(id);
        ...
    }

I need to convert the id parameter to a Long and I have tried to do this with Long.valueOf(stringId) and Long.parseLong(stringId) but when I call the route with the parameter id as 1, I get this error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: For input string: "1
"] with root cause

java.lang.NumberFormatException: For input string: "1

I call the route from a form like this:

<form method="POST" action="addItem">
            <div class="form-group">
                <table id="details" class="table table-responsive">
                    <tr id="type">
                        <th class="text-left select-title">Type* :</th>
                        <td>
                            <select name="id" id="input" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>   
                    </tr>
                    <tr id="component1">
                        <th class="text-left select-title">Type of Component * :</th>
                        <td>
                            <select id="select" name="component1" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>
                        <th class="text-left quant-title">Quantity * :</th>
                        <td class="quantity-field">
                            <input name="quantity1" type="number" step="any" placeholder="Quantity" class="quantity-input" required>
                        </td>
                    </tr>
                    <tr class="table-row">
                        <td>
                            <button onclick="addComponent()" id="add-component" class="btn btn-md btn-outline-success"> + Add Component</button><br>
                        </td>
                        <td>
                            <button onclick="removeComponent()" id="rem-component" class="btn btn-md btn-outline-danger"> - Remove Component</button><br>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <input type="submit" name="submit" id="submit">
                        </th>
                    </tr>
                  </table>
                </div>
</form>

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?

You said the string is "1\r\n" .

However, Long.valueOf() only works strings that only consist of numbers.

In order to fix this, use trim() :

Long.valueOf(stringId.trim());

I would recommend you to use Long#parseLong instead of Long#valueOf as it returns long instead of Long . That gets rid of useless object creation.

@RequestMapping(value = "/addItem", method = RequestMethod.POST)
public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) {
    String stringId = allRequestParams.get("id");
    System.out.println(stringId);
    long id = Long.parseLong(stringId.trim());
    System.out.println(id);
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM