Getting an NumberFormat exception from a form submission that passes a phone number. However, the number passed is in fact a number and I convert it to an int within the servlet. Within the error message you can see that an int was entered in – 8456640236.
Servlet Method
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
Contact newComment = new Contact();
newComment.setName(request.getParameter("nameParameter"));
newComment.setEmail(request.getParameter("emailParameter"));
newComment.setPhone(Integer.parseInt(request.getParameter("phoneParameter")));
newComment.setComment(request.getParameter("commentParameter"));
request.setAttribute("commentAttribute", newComment);
RequestDispatcher view = request.getRequestDispatcher("/successComment.jsp");
view.forward(request, response);
}
Error Message
java.lang.NumberFormatException: For input string: "8456640236"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
java.lang.Integer.parseInt(Integer.java:461)
java.lang.Integer.parseInt(Integer.java:499)
org.test.Servlet.doPost(Servlet.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
The value you’re trying to parse is larger than an
Integercan hold, 231-1, which is 2147483647.You’d need to make it a
Long(orlong) instead and useLong#parseLong()to parse it.Note that this concrete problem has nothing to do with JSP/Servlets, but anything with basic Java. You’d have exactly the same problem when doing so in a plain vanilla Java class with a
main()method and hardcoded values (which would have been easier for you to isolate the problem).