I want to check if a
Integer
Integer
<input name="varsta" placeHolder="Varsta:" type="text" data-constraints='@NotEmpty @Required @AlphaSpecial'> <br/><br/>
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();
}
}
Class Integer
is just an wrapper on top of primitive int
type. So it can either be null
or store a valid integer value. There is no obvious "empty" definition for it.
If you just compare Integer
against empty String
, you''ll get false
as a result. Always. See Integer.equals(Object o)
implementation:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
First of all, you can get a NumberFormatException
during parsing integer in the line:
Integer varsta = Integer.parseInt(request.getParameter("varsta"));
And you are getting it, since For input string: ""
looks like a NumberFormatExpection
message.
In your example you should either check whether the "varsta"
attribute value is a number (assume it's a string) and then parse it, or parse it as is and catch NumberFormatException
that Integer.parseInt()
throws on incorrect argument.
First solution:
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);
}
Second solution:
Integer varsta = null;
String varstaStr = request.getParameter("varsta"); // read string 'varsta' field
try {
varsta = Integer.parseInt(varsta);
} catch (NumberFormatException e) {
// handle error
}
After this, you have one more problem in the line:
if(varsta == null && "".equals(varsta)){
The varsta
reference has type Integer
here, so "".equals(varsta)
will always return false
:
(varsta == null && "".equals(varsta)) = [assume varsta is null] =
((null) == null && "".equals(null)) = (true && false) = false
Replace
if(varsta == null && "".equals(varsta)){
with
if(varsta == null){
This should help you.
P.S. If you use Java of version 7 or higher, consider use try-with-resources to manage Connection
and PreparedStatement
.