about NumberFormatException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • om anand giri
    New Member
    • Aug 2010
    • 4

    about NumberFormatException

    Hello to all,
    I have a problem about NumberFormatExc eption and i m very sad because i m not able to solve this.
    So please help.

    The code is----------
    Code:
    <code>
    
    
    
    <%@page import="Connection.Connect" %>
    <%@page import="java.sql.*" %>   <%-- for SQL Statements--%>
    <%@page import="java.util.*" %>
    <%@page import="java.math.BigInteger"%>
    <%@page import="java.math.*" %>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>credit report</title>
        </head>
        <body>
            <form action="Logout.jsp" name="Submit" method="POST">
                 <table border="0" cellspacing="0" width="100%">
                    <tbody>
                         <tr>
    
    
                            <td><font size="8"><b>Automatic tailer System</b></font></td>
    
                             <td align="right"><input type="submit" value="LOGOUT" name="LGB" /></td>
    
                         </tr>
    
                     </tbody>
                 </table>
             </form>
    
    
            <div align="center" style=" background-color: black ">
                <font color="white"><b>Employee ID : </b><%= session.getAttribute("ID") %> </font> &nbsp;&nbsp;&nbsp;&nbsp;
                <font color="white"><b>Branch : </b> <%= session.getAttribute("branch") %></font> &nbsp;&nbsp;&nbsp;&nbsp;
            </div>
    
            <%
    
                 String ID2=session.getAttribute("ID2").toString().trim();
                 String name=session.getAttribute("name").toString().trim();
                 String am=request.getParameter("amount").trim();
                 int amt=Integer.parseInt(am);
                 String cred=request.getParameter("credited").trim();
    
    try
        {
            Connection con3=null;
            String statement3="Select * from MASTER where ID = '" + ID2 + "'";
            Connect newconn3 = new Connect();
            ResultSet result3=null;
            Statement command3 = null;
            con3 = newconn3.create();
            command3 = con3.createStatement();
            result3 = command3.executeQuery(statement3);
            result3.next();
           String IDreq = result3.getString("ID").trim();
            String acc_t= result3.getString("ACCOUNT_TYPE").trim();
           int tb=result3.getInt("TOTAL_BAL");
            tb=tb+amt;
    
    
    
    
           String Cr;
    
    
                Connection con=null;
                String getreq="SELECT MAX(TRAN_ID) FROM CREDIT";
                Connect newconn = new Connect();
                ResultSet result=null;
                Statement command = null;
                con = newconn.create();
                command = con.createStatement();
                result = command.executeQuery(getreq);
                result.next();
                String find = String.valueOf(result.getBigDecimal(1));
    
                String nextreqcom="";
                int request_id;
                java.util.Date curr = new java.util.Date();
    
                int cyear = curr.getYear()+1900;
                int cdate = curr.getDate();
                int cmonth = curr.getMonth()+1;
    
               if((find.length()==4)||(find.equals(null)))
                {
                    find="000001";
                    if(cmonth<10)
                    {
                        nextreqcom = cyear+"0"+cmonth+""+cdate+""+find;
                    }
                    if(cdate<10)
                    {
                        nextreqcom = cyear+""+cmonth+"0"+cdate+""+find;
                    }
                    if((cmonth<10)&&(cdate<10))
                    {
                        nextreqcom = cyear+"0"+cmonth+"0"+cdate+""+find;
                    }
                   request_id =Integer.parseInt((nextreqcom).trim());
                  // request_id = new BigInteger(nextreqcom);
                }
               else
                {
    
    
    
                    int maxreq = Integer.parseInt(find.substring(8));
                    int nextreq=maxreq+1;
    
                    String chk = String.valueOf(nextreq);
                    String valuereq="";
                    switch(chk.length())
                    {
                        case 1 : valuereq = "00000"+nextreq;
                        break;
                        case 2 : valuereq = "0000"+nextreq;
                        break;
                        case 3 : valuereq = "000"+nextreq;
                        break;
                        case 4 : valuereq = "00"+nextreq;
                        break;
                        case 5 : valuereq = "0"+nextreq;
                        break;
                        case 6 : valuereq = ""+nextreq;
                        break;
    
                    }
                    if(cmonth<10)
                    {
                        nextreqcom = cyear+"0"+cmonth+""+cdate+""+valuereq;
                    }
                    if(cdate<10)
                    {
                        nextreqcom = cyear+""+cmonth+"0"+cdate+""+valuereq;
                    }
                    if((cmonth<10)&&(cdate<10))
                    {
                        nextreqcom = cyear+"0"+cmonth+"0"+cdate+""+valuereq;
                    }
                   request_id =Integer.parseInt((nextreqcom).trim());
                   //  request_id = new BigInteger(nextreqcom);
    
                }
    
                java.util.Date ff = new java.util.Date();
                int mm = ff.getMonth()+1;
                int dd = ff.getDate();
                int yyyy = ff.getYear()+1900;
                String today = yyyy+"-"+mm+"-"+dd;
    
                 Connection con2 = newconn.create();
                Statement command2 = con2.createStatement();
                String insert = "INSERT INTO CREDIT(TRAN_ID,ID,NAME,ACCOUNT_TYPE,AMOUNT,CREDITED_BY,TOTAL_BAL,CURT,TRAN_TYPE) values('"+request_id+"','"+IDreq+"','"+name+"','"+acc_t+"','"+amt+"','"+cred+"','"+tb+"','"+today+"','Cr')";
                int set = command2.executeUpdate(insert);
                String show = "REQV"+request_id;
    
    
    
                Connection con1=null;
                con1 = newconn.create();
                Statement command1 = con1.createStatement();
                 String update = "UPDATE MASTER SET TOTAL_BAL='"+tb+"' WHERE ID='"+IDreq+"'";
                int set1 = command1.executeUpdate(update);
            %>
    
             <BR>
            <fieldset><legend><font face="Verdana" size="5">SYSTEM REPORT</font></legend>
    
    
    <table border=0 width=100% height=100%><tr><td>
    
    <table width=50%>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td align="center"><font face="Courier" size="3">The amount is credited in respective account.</font></td></tr>
    
    
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    
    <tr><td align="center"><font face="Courier" size="3">Transaction ID is: <b><%=show%></b></font></td></tr>
    
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
    </table>
    
    
    </td></tr></table>
            </fieldset>
    
    <BR>
    <form name="Employee" action="Employee.jsp" method="POST">
        <div style="width:100%">
    <table style="width:100%">
    <tr>
    <td align="center" style="width:20%"><input style="width:20%" type="submit" value="OK" name="ID"></td>
    </tr>
    </table>
        </div>
    </form>
    
    
            <%
    
             }
        catch(Exception e)
                { %> <%=e.toString() %>
               
               <%
        }
             %>
        </body>
    </html>
    
    
    </code>
    and error is ----

    java.lang.Numbe rFormatExceptio n: For input string: "20100819000001 "


    Thanks in advance.
    Last edited by Niheel; Aug 24 '10, 04:44 PM.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    The number is too big to fit into an integer.

    "System.out.pri ntln(Integer.MA X_VALUE);" will give you 2147483647. Because it is a 32 bit signed type.

    You can use class java.math.BigIn teger to store bigger numbers.

    By the way, I can't see which statement caused the error. You only listed the jsp-code. Please list the corresponding Java-code (remember: jasper of Tomcat compiles every jsp-page into class-files and puts the corresponding java-code in a special directory. For example by default, "test.jsp" would generate "test_jsp.j ava" Just grab it from there and list it here, together with the line number where the error happened.
    And most important, please use code-tags around your code this time when you list it here!

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Code:
      20100819000001
      It looks like date to me 2010 08 19 00 00 01. What you want to do with it?
      If you want to convert this into an integer, you can't convert. See chaarmann's post.

      Can you post the section of code which give this error? And please use code tag while posting.

      Regards
      Dheeraj Joshi

      Comment

      • om anand giri
        New Member
        • Aug 2010
        • 4

        #4
        sir,
        i m alredy used class biginteger.
        but my problem is not solved.
        I m using glassfish derby server and javadb.
        I am doing project on ATM web application, and when a customer credit money in his account then a reference id is generated. this is that number that is a unique number.
        i m able to generate this but i m not able to save this in database.
        i m using datatype bigint in javadb.
        but this exception is generated and value is not going to database.
        if u have any other idea or code then please send it to me. i m not understand, where that file allocated which u want.
        Sir, please hepl me abut this.
        How can i generate this number because the desired id format is same as given.

        Thanks.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          You wrote "i m alredy used class biginteger". That's right, but it's commented out in the code above. So it's not currently used. What is the problem if you use it? You shouldn't get a NumberFormatExc eption if you use it, so maybe the error happens somewhere else where you don't use it, but should do.

          You wrote "i m not understand, where that file allocated which u want."
          Please look in your tomcat-work-directory. The direct path depends where you installed Tomcat. My tomcat installation is in "C:\Program Files\ApacheTom cat". You will find some directories named "bin" (executable of tomcat), "logs" (your logs) "webapps" (where your jsp-files are) and also "work". In subdirectory "Catalina\local host\_" (Yes, the directory name is an underscore) of your "work"-directory is all the java-code located that tomcat generated. Here you will find the java-file that corresponds to the jsp-file you listed above. If you can't find it there, then search one directory higher by application name ("Catalina\loca lhost\(myApplic ation)". If you can't find it there, the search the whole work-directory, listing all files by date. The wanted file is the one which was just gererated, that means with the latest date.
          Please remember to use code tags if you list this java code here, along with the error-line number from the log.

          You wrote "Sir, please hepl me abut this."
          In order to help you, it is very important to know the line number where the error happened. This also will help you much in your own future error analysis.
          So if you still have problems to grab the file and line of error, then rewrite your code.
          You have to rewrite it anyway, because you are not following MVC coding standard and it will never pass QA: Your jsp-page should only contain the view-logic (html-commands) and not the business-logic (database access). You must put all your business-logic into a Java Action-class (if using Struts) or into a java-servlet. And if you compile that java-servlet manually, the compiler right away will give you the line number and error.

          By the way, your code:
          Code:
          String chk = String.valueOf(nextreq);
          String valuereq="";
          switch(chk.length())
          {
          case 1 : valuereq = "00000"+nextreq;
          break;
          case 2 : valuereq = "0000"+nextreq;
          break;
          case 3 : valuereq = "000"+nextreq;
          break;
          case 4 : valuereq = "00"+nextreq;
          break;
          case 5 : valuereq = "0"+nextreq;
          break;
          case 6 : valuereq = ""+nextreq;
          break;
          could be simply rewritten as:
          Code:
          final String leadingZeros = "000000";
          String valuereq = leadingZeros + nextreq;
          valuereq = valuereq.substring(valuereq.length() - leadingZeros.length());

          Comment

          • om anand giri
            New Member
            • Aug 2010
            • 4

            #6
            Respected Sir,
            Thanks for your suggestions.
            I have solved my problem and these codes work properly.
            In future i am always ask for your help.So please help me if i want in future.

            Thanks.

            Comment

            • om anand giri
              New Member
              • Aug 2010
              • 4

              #7
              about numberformatexc eption

              Sir,
              I have a new problem.
              Actually i was design this code to handle the reference number and i want it to reset when date is changed.
              As at present date it is "CR201008240000 07" and at next day it is "CR201008250000 08".
              But i want it as "CR201008250000 01".
              How could it possible by this code?
              Please help me about this new problem.
              Attached Files

              Comment

              • chaarmann
                Recognized Expert Contributor
                • Nov 2007
                • 785

                #8
                Can you please tell us what was the exact problem at last and how you have solved your problem? In listing your properly working code or your solution here, that means sharing your knowledge, many other readers will benefit from the solution, in the same way you benefited from the knowledge I shared.

                Also please open a new thread for your new problem, and don't put the source code as attachment, instead paste it directly and wrap code-tags around it (just select the whole code and use the #-button of the message-editor)

                Comment

                Working...