473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to increment serial number?

hi! i was doing wondering if there's a way to print out & increment 3-digit-serial no.?
i was planning to print out an invoice.
i want the starting of the invoice to be in this format YYYYMMDDNNN (where YYYY is the year, MM is month, DD is date & NNN is serial no (e.g: 001, 002, 003....) )

i've did the Year, month and date already. but when i want to print out the serial no. , it shows "1, 2 ..." instead of "001, 002"..

can someone help me?

this is the code i did so far.


Expand|Select|Wrap|Line Numbers
  1. package JPRG;
  2. import java.util.Calendar;
  3.  
  4. public class ShoppingTransactionRecord {
  5.  
  6.     public static void main (String args[]){
  7.  
  8.       int serialNo = 000;
  9.  
  10.       Calendar cal = Calendar.getInstance();
  11.  
  12.       int date = cal.get(Calendar.DATE);
  13.       int month = cal.get(Calendar.MONTH) + 1;
  14.       int year = cal.get(Calendar.YEAR);
  15.  
  16.  
  17.       for (int i=0;i<10;i++){
  18.           serialNo++;
  19.     System.out.println("Testing =====> " + year + month + date + serialNo);
  20.       }
  21.    }
  22.  
  23. }
  24.  
  25.  
Jan 24 '11 #1

✓ answered by horace1

you can use Java formatting
http://download.oracle.com/javase/1....Formatter.html
http://java.sun.com/developer/techni...mming/sprintf/

e.g.
Expand|Select|Wrap|Line Numbers
  1.     System.out.printf("Testing =====> %d%d%d%03d\n",  year , month , date , serialNo);
  2.  
gives
Expand|Select|Wrap|Line Numbers
  1. Testing =====> 2011124001
  2. Testing =====> 2011124002
  3. Testing =====> 2011124003
  4. Testing =====> 2011124004
  5. Testing =====> 2011124005
  6. Testing =====> 2011124006
  7. Testing =====> 2011124007
  8. Testing =====> 2011124008
  9. Testing =====> 2011124009
  10. Testing =====> 2011124010
you can set the field width for the other parameters to ensure the output is what you require

2 16907
horace1
1,510 Expert 1GB
you can use Java formatting
http://download.oracle.com/javase/1....Formatter.html
http://java.sun.com/developer/techni...mming/sprintf/

e.g.
Expand|Select|Wrap|Line Numbers
  1.     System.out.printf("Testing =====> %d%d%d%03d\n",  year , month , date , serialNo);
  2.  
gives
Expand|Select|Wrap|Line Numbers
  1. Testing =====> 2011124001
  2. Testing =====> 2011124002
  3. Testing =====> 2011124003
  4. Testing =====> 2011124004
  5. Testing =====> 2011124005
  6. Testing =====> 2011124006
  7. Testing =====> 2011124007
  8. Testing =====> 2011124008
  9. Testing =====> 2011124009
  10. Testing =====> 2011124010
you can set the field width for the other parameters to ensure the output is what you require
Jan 24 '11 #2
Oralloy
988 Expert 512MB
rawringbuny,

I think you want to use the static method String.format, with a format similar to the one in the example from horace1.

Luck!
Oralloy
Jan 24 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: tom | last post by:
Hi, How do I get the serial number of the harddisk in .NET? I want this to be the same number even if the user has reformatted, so I do not want the volume serial number. Thanx, t
5
by: | last post by:
Hi, Do memory sticks have serial numbers like harddrives? If so how can I get this, I want to uniquely identify a memory stick (removable drive) for authentication. Thanks
79
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current...
3
by: Mark Harris | last post by:
I have an installer which uses a Customer Information page in it, is there an easy way to pass the serial number entered to a custom action? If not, where would i find the serial number in the...
14
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)...
9
by: Nebojsa4 | last post by:
Hi. First, sorry on my weak English to all. Qusetion: How to read (in VB) Manufacturer serial number of Hard disk drive? Not volume/serial number of C:, D:, etc. partitons. For reading...
8
chandru8
by: chandru8 | last post by:
hi every body i need a logic , that for a particular code i need to genrate a serial number for a day should be one for the next day it should be increment to 2 like this can any one help me...
1
by: Limno | last post by:
Hi all Can anyone tell how to increment the serial number, now i m displaying $i value for that, its displaying 1,2,3,4,5. if next set of question again it displaying 1,2,3,4,5 But i need for 1st...
1
by: rajsrmc | last post by:
Hello Guys! I have a mysql table which is connected with aspx page in grid view, in the connected table i have assigned Serial number column as Primary key and as well in auto increment, by using...
1
by: Keshia | last post by:
I have a database that is for keeping track of inventory. I need to have my form for entering new inventory to automatically make a new serial number based on the category of the new item. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.