gpt4 book ai didi

java - 检查输入空

转载 作者:行者123 更新时间:2023-11-29 09:49:59 24 4
gpt4 key购买 nike

我发现如果输入不是数字,这行代码会出错

int sum = Integer.parseInt(request.getParameter("sum"));

错误信息是

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a"
root cause

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

如果输入是字符串或null,如何处理输入?

谢谢

最佳答案

您应该首先确保请求参数不为空且仅包含数字,使用:

if (request.getParameter("sum") != null &&
request.getParameter("sum").matches("^\\d+$"))
int sum = Integer.parseInt(request.getParameter("sum"));

关于java - 检查输入空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9459767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com