Kacper Zięba
Kacper Zięba

Reputation: 43

java.lang.NumberFormatException: For input string: "" - When I try to parse empty string as integer

I wanna make a servlet where you write your name, surname, email address and you have to enter the correct sum of two numbers.

I've got one problem. When I don't enter anything in this "captcha", it gives me HTTP Status 500, Message: HTTP Status 500

Exception:

java.lang.NumberFormatException: For input string: ""
    java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    java.base/java.lang.Integer.parseInt(Integer.java:662)
    java.base/java.lang.Integer.parseInt(Integer.java:770)
    pl.coderslab.Sess05.doPost(Sess05.java:16)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

I'd like it to return to the form so you have to type the captcha (I'm not checking the other fields, don't pay attention to it).

Here's my code:

@WebServlet("/Sess05")
public class Sess05 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession sess = request.getSession();
        int userSum = Integer.parseInt(request.getParameter("captchaInput"));   //Here's 16 line
        int correctSum = (int) sess.getAttribute("captcha");
        if (userSum != correctSum || request.getParameter("captchaInput").equals("")) {
            Random randGenerator = new Random();
            int firstRand = randGenerator.nextInt(101);
            int secRand = randGenerator.nextInt(101);
            int sum = firstRand + secRand;
            sess.setAttribute("captcha", sum);

            response.getWriter().append("<form action='#' method='post'>");
            response.getWriter().append("<label>Name: <input type='text' name='name'></label><br>");
            response.getWriter().append("<label>Surname: <input type='text' name='surname'></label><br>");
            response.getWriter().append("<label>Email: <input type='email' name='email'></label><br>");
            response.getWriter().append("<label>Enter the sum " + firstRand + " + " + secRand + "<input type='number' name='captchaInput'></label><br>");
            response.getWriter().append("<input type='submit' value='Submit'>");
            response.getWriter().append("</form>");
            response.getWriter().append("Enter the correct sum!");
        } else {
            response.getWriter().append("Sum is correct!");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Random randGenerator = new Random();
        int firstRand = randGenerator.nextInt(101);
        int secRand = randGenerator.nextInt(101);
        int sum = firstRand + secRand;
        HttpSession sess = request.getSession();
        sess.setAttribute("captcha", sum);
        
        response.getWriter().append("<form action='#' method='post'>");
        response.getWriter().append("<label>Name: <input type='text' name='name'></label><br>");
        response.getWriter().append("<label>Surname: <input type='text' name='surname'></label><br>");
        response.getWriter().append("<label>Email: <input type='email' name='email'></label><br>");
        response.getWriter().append("<label>Enter the sum " + firstRand + " + " + secRand + "<input type='number' name='captchaInput'></label><br>");
        response.getWriter().append("<input type='submit' value='Submit'>");
        response.getWriter().append("</form>");
    }
}

Upvotes: 0

Views: 1838

Answers (2)

Anish B.
Anish B.

Reputation: 16539

Update line number 16 :

  String value = request.getParameter("captchaInput");
  int userSum = value == null || value.isEmpty() ? userSum : Integer.parseInt(value);

Upvotes: 0

Mohamed Atef
Mohamed Atef

Reputation: 21

The main problem is caused because you are trying to get integer value from empty variable which is not number so to fix the problem you should check if that parameter contains value and it is a numeric value by creating method like below in your code :

public static boolean isNumeric(String strNum) {
    if (strNum == null) {
        return false;
    }
    try {
        int d = Integer.parseInt(strNum);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

and then call this method in your code to check parameter value before doing anything and do whatever you want based on this boolean:

 @WebServlet("/Sess05")
    public class Sess05 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession sess = request.getSession();
//  check if it is numeric or not           
 boolean isNumeric = Sess05.isNumeric(request.getParameter("captchaInput"));
if(isNumeric){
  // do your servlet logic 
}else {
  // do whatever you will do if you request hasn't a numeric value 
}
    

Upvotes: 1

Related Questions