我想检查一个Integer
值是否为空。的值以Integer
表格形式填写。这是我的代码。
这里引入了值:
<input name="varsta" placeHolder="Varsta:" type="text" data-constraints='@NotEmpty @Required @AlphaSpecial'> <br/><br/>
Run Code Online (Sandbox Code Playgroud)
现在我想检查是否有数据引入。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Integer varsta = Integer.parseInt(request.getParameter("varsta"));
request.getSession().setAttribute("varsta", varsta);
try {
DBConnection connection = new DBConnection();
Connection con = connection.Connect();
PreparedStatement ps = con.prepareStatement(
"insert into user(Nume,Prenume,E_mail,Parola,Varsta,Sex,Greutate,Inaltime,Nivel_activitate,Calcul_calorii)" +
"values ('"+nume+"','"+prenume+"','"+email1+"','"+parola+"','"+varsta+"','"+sex+"','"+greutate+"','"+inaltime+"','"+activitate+"','"+calorii+"')"
);
if (varsta == null && "".equals(varsta)) {
String message = "Va rugam completati cu atentie TOATE campurile!";
request.setAttribute("message", message);
request.getRequestDispatcher("/inregistrare.jsp").forward(request, response);
} else {
int i = ps.executeUpdate();
if (i > 0) {
request.getRequestDispatcher("/preferinte.jsp").forward(request, response);
}
}
ps.close();
con.close();
} catch(Exception se) {
se.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
不管用。
有人可以帮助我吗?
类Integer
只是原始int
类型之上的包装器。所以它可以是null
或存储一个有效的整数值。它没有明显的“空”定义。
如果您只是Integer
与 empty进行比较String
,您就会得到false
结果。总是。见Integer.equals(Object o)
实现:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
首先,您可以NumberFormatException
在行中解析整数期间获得一个:
Integer varsta = Integer.parseInt(request.getParameter("varsta"));
Run Code Online (Sandbox Code Playgroud)
你明白了,因为For input string: ""
看起来像一条NumberFormatExpection
消息。
在您的例子您应该检查是否"varsta"
属性值是一个数(假设它是一个字符串),然后分析它,或解析它是和捕获NumberFormatException
的是Integer.parseInt()
不正确的说法抛出。
第一个解决方案:
Integer varsta = null;
String varstaStr = request.getParameter("varsta"); // read string 'varsta' field
if (varstaStr != null && varstaStr.matches("\\d+")) { // null-check and regex check to make sure the string contains only digits
varsta = Integer.parseInt(varstaStr);
}
Run Code Online (Sandbox Code Playgroud)
第二种解决方案:
Integer varsta = null;
String varstaStr = request.getParameter("varsta"); // read string 'varsta' field
try {
varsta = Integer.parseInt(varsta);
} catch (NumberFormatException e) {
// handle error
}
Run Code Online (Sandbox Code Playgroud)
在此之后,您还有一个问题:
if(varsta == null && "".equals(varsta)){
Run Code Online (Sandbox Code Playgroud)
该varsta
参考具有类型Integer
在这里,所以"".equals(varsta)
总是返回false
:
(varsta == null && "".equals(varsta)) = [assume varsta is null] =
((null) == null && "".equals(null)) = (true && false) = false
Run Code Online (Sandbox Code Playgroud)
代替
if(varsta == null && "".equals(varsta)){
Run Code Online (Sandbox Code Playgroud)
和
if(varsta == null){
Run Code Online (Sandbox Code Playgroud)
这应该对你有帮助。
PS如果你使用Java 7或更高版本,可以考虑使用try-with-resources来管理Connection
和PreparedStatement
。
归档时间: |
|
查看次数: |
62812 次 |
最近记录: |