Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    How to generate and increment a number one by one?

    I am trying to generate and store an id like AAES001 in the mysql db. It will increment like AAES001, AAES002,AAES003.. and so on. However its giving me an error:
    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    	at com.inv.dao.AAESOrderDAO.createAAESOrder(AAESOrderDAO.java:33)
    	at com.inv.controller.AAESOrderCtrl.recordSurvey(AAESOrderCtrl.java:233)
    	at com.inv.controller.AAESOrderCtrl.doPost(AAESOrderCtrl.java:80)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:662)
    Please Help

    Criteria criteria = session.createCriteria(AAESOrder.class);
    Integer maxLength = (Integer)criteria.uniqueResult();
    			    aaesOrder.setAAESNo("AAES00"+String.valueOf((maxLength == null ? 0 : maxLength) + 1));			    

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Of course that would throw an error. AAES001 is not a number.
    You'll need to split off your string component from the numerical component. Then you can cast the numerical component using Integer.parseInt and increment that part. Then re-assemble the string with a 0 padded buffer. Alternatively, you can pick away at a char array and increment each part as required.
    You could also work with prefix and suffix components at the data level and keep the prefix separate from the suffix. That would make it easier to work with but more work to deal with.
    On a side note, if there were no 'S' in there, you could cast it to a hex number and increment it. Whether that would be ideal behaviour or not would depend on your business rules.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts