I got NumberFormatException in the below code , Please analyze that need do favor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pradhi11
    New Member
    • Jul 2014
    • 1

    I got NumberFormatException in the below code , Please analyze that need do favor

    Can i get the values from the same JSP page for further calculation.

    Example:
    Code:
    <html> <form>
    Enter ID:<input type="text" name="id"> <%
        String id=request.getParameter("id");
         int no=Integer.ParseInt(id);
    :%> </form> </html>
    For the above code I got NumberFormatExc eption if it is possible or any other way to achive this in same jsp file.

    Thanks in advance
    Last edited by Rabbit; Jul 5 '14, 05:10 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • rajujrk
    New Member
    • Aug 2008
    • 107

    #2
    Use the below Code

    Code:
     <html> <form>
        Enter ID:<input type="text" name="id"> <%
           [B] String id=(request.getParameter("id")!=null)?request.getParameter("id"):"0";[/B]
             int no=Integer.ParseInt(id);
        :%> </form> </html>

    Thanks
    Raju

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      Integer.ParseIn t(id) throws a NumberFormatExc eption if the parameter given does not contain a number. So the code given by Raju does not fix this problem.
      For example:

      id="Hello";
      Integer.ParseIn t(id);

      So you need to surround your code with a try - catch statement, and in case no number is given you need to do some error handling (e.g. popping up a box with "Please enter a valid number" and focussing on the input field)

      Comment

      Working...