Search code examples
javanumberformatexception

Need help! How to solve this exception


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

this is how my Java code looks like:

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

Solution

  • If request parameter age is empty, not a number, or missing altogether, you cannot convert it to a number.

    You'll have to decide what to do in such situation, then do it in the cases mentioned above, i.e.:

    • Check the parameter for non-emptiness
    • Surround the conversion with try { ... } catch (NumberFormatException e) { ... }