简体   繁体   English

在 java 中将字符串转换为长字符串

[英]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.我正在使用 spring 引导项目,我正在尝试在 controller 中创建一个与动态数量的参数一起使用的路由。 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:我需要将 id 参数转换为Long并且我尝试使用Long.valueOf(stringId)Long.parseLong(stringId)执行此操作,但是当我调用参数id为 1 的路由时,我收到此错误:

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. controller 路由中的打印语句用于调试,仅打印第一个打印语句。 How can I correctly convert the string to a long?如何正确地将字符串转换为长字符串?

You said the string is "1\r\n" .你说字符串是"1\r\n"

However, Long.valueOf() only works strings that only consist of numbers.但是, Long.valueOf()仅适用于仅由数字组成的字符串。

In order to fix this, use trim() :为了解决这个问题,使用trim()

Long.valueOf(stringId.trim());

I would recommend you to use Long#parseLong instead of Long#valueOf as it returns long instead of Long .我建议您使用Long#parseLong而不是Long#valueOf因为它返回long而不是Long That gets rid of useless object creation.这摆脱了无用的 object 创建。

@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);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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